I'm new to PyQt and I'm trying to create a videoplayer. When the user screenshots a certain frame, I should be able to retrieve 5 frames prior to the current frame and another 5 frames after the current frame. So far I'm only able to retrieve the current frame and I'm having a hard time figuring out which part of the code to alter to store the previous frames and the after frames. Here's the complete code:
# PyQt5 Video player
#!/usr/bin/env python
from PyQt5.QtCore import QDir, Qt, QUrl, pyqtSignal, QPoint, QRect, QObject
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QVideoFrame, QAbstractVideoSurface, QAbstractVideoBuffer, QVideoSurfaceFormat
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon, QPainter, QImage
import sys
import os
import os.path as osp
class VideoFrameGrabber(QAbstractVideoSurface):
frameAvailable = pyqtSignal(QImage)
def __init__(self, widget: QWidget, parent: QObject):
super().__init__(parent)
self.widget = widget
def supportedPixelFormats(self, handleType):
return [QVideoFrame.Format_ARGB32, QVideoFrame.Format_ARGB32_Premultiplied,
QVideoFrame.Format_RGB32, QVideoFrame.Format_RGB24, QVideoFrame.Format_RGB565,
QVideoFrame.Format_RGB555, QVideoFrame.Format_ARGB8565_Premultiplied,
QVideoFrame.Format_BGRA32, QVideoFrame.Format_BGRA32_Premultiplied, QVideoFrame.Format_BGR32,
QVideoFrame.Format_BGR24, QVideoFrame.Format_BGR565, QVideoFrame.Format_BGR555,
QVideoFrame.Format_BGRA5658_Premultiplied, QVideoFrame.Format_AYUV444,
QVideoFrame.Format_AYUV444_Premultiplied, QVideoFrame.Format_YUV444,
QVideoFrame.Format_YUV420P, QVideoFrame.Format_YV12, QVideoFrame.Format_UYVY,
QVideoFrame.Format_YUYV, QVideoFrame.Format_NV12, QVideoFrame.Format_NV21,
QVideoFrame.Format_IMC1, QVideoFrame.Format_IMC2, QVideoFrame.Format_IMC3,
QVideoFrame.Format_IMC4, QVideoFrame.Format_Y8, QVideoFrame.Format_Y16,
QVideoFrame.Format_Jpeg, QVideoFrame.Format_CameraRaw, QVideoFrame.Format_AdobeDng]
def isFormatSupported(self, format):
imageFormat = QVideoFrame.imageFormatFromPixelFormat(format.pixelFormat())
size = format.frameSize()
return imageFormat != QImage.Format_Invalid and not size.isEmpty() and \
format.handleType() == QAbstractVideoBuffer.NoHandle
def start(self, format: QVideoSurfaceFormat):
imageFormat = QVideoFrame.imageFormatFromPixelFormat(format.pixelFormat())
size = format.frameSize()
if imageFormat != QImage.Format_Invalid and not size.isEmpty():
self.imageFormat = imageFormat
self.imageSize = size
self.sourceRect = format.viewport()
super().start(format)
self.widget.updateGeometry()
self.updateVideoRect()
return True
else:
return False
def stop(self):
self.currentFrame = QVideoFrame()
self.targetRect = QRect()
super().stop()
self.widget.update()
def present(self, frame):
if frame.isValid():
cloneFrame = QVideoFrame(frame)
cloneFrame.map(QAbstractVideoBuffer.ReadOnly)
image = QImage(cloneFrame.bits(), cloneFrame.width(), cloneFrame.height(),
QVideoFrame.imageFormatFromPixelFormat(cloneFrame.pixelFormat()))
self.frameAvailable.emit(image) # this is very important
cloneFrame.unmap()
if self.surfaceFormat().pixelFormat() != frame.pixelFormat() or \
self.surfaceFormat().frameSize() != frame.size():
self.setError(QAbstractVideoSurface.IncorrectFormatError)
self.stop()
return False
else:
self.currentFrame = frame
self.widget.repaint(self.targetRect)
return True
def updateVideoRect(self):
size = self.surfaceFormat().sizeHint()
size.scale(self.widget.size().boundedTo(size), Qt.KeepAspectRatio)
self.targetRect = QRect(QPoint(0, 0), size)
self.targetRect.moveCenter(self.widget.rect().center())
def paint(self, painter):
if self.currentFrame.map(QAbstractVideoBuffer.ReadOnly):
oldTransform = self.painter.transform()
if self.surfaceFormat().scanLineDirection() == QVideoSurfaceFormat.BottomToTop:
self.painter.scale(1, -1)
self.painter.translate(0, -self.widget.height())
image = QImage(self.currentFrame.bits(), self.currentFrame.width(), self.currentFrame.height(),
self.currentFrame.bytesPerLine(), self.imageFormat)
self.painter.drawImage(self.targetRect, image, self.sourceRect)
self.painter.setTransform(oldTransform)
self.currentFrame.unmap()
class VideoWindow(QMainWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
self.setWindowTitle("PyQt Video Player Widget")
self.counter = 0
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoWidget = QVideoWidget()
self.videoFrame = QVideoFrame()
self.playButton = QPushButton()
self.playButton.setEnabled(False)
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playButton.clicked.connect(self.play)
self.positionSlider = QSlider(Qt.Horizontal)
self.positionSlider.setRange(0, 0)
self.positionSlider.sliderMoved.connect(self.setPosition)
self.errorLabel = QLabel()
self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Maximum)
# Create new action
openAction = QAction(QIcon('open.png'), '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open video')
openAction.triggered.connect(self.openFile)
# Create exit action
exitAction = QAction(QIcon('quit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.exitCall)
screenshotAction = QAction(QIcon('screenshot.png'), '&Screenshot', self)
screenshotAction.setShortcut('Ctrl+S')
screenshotAction.setStatusTip('Screenshot scenes')
screenshotAction.triggered.connect(self.screenshotCall)
# Create menu bar and add action
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
#fileMenu.addAction(newAction)
fileMenu.addAction(openAction)
fileMenu.addAction(screenshotAction)
fileMenu.addAction(exitAction)
# Create a widget for window contents
wid = QWidget(self)
self.setCentralWidget(wid)
# Create layouts to place inside widget
controlLayout = QHBoxLayout()
controlLayout.setContentsMargins(0, 0, 0, 0)
controlLayout.addWidget(self.playButton)
controlLayout.addWidget(self.positionSlider)
layout = QVBoxLayout()
layout.addWidget(self.videoWidget)
layout.addLayout(controlLayout)
layout.addWidget(self.errorLabel)
# Set widget to contain window contents
wid.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoWidget)
self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
self.mediaPlayer.positionChanged.connect(self.positionChanged)
self.mediaPlayer.durationChanged.connect(self.durationChanged)
self.mediaPlayer.error.connect(self.handleError)
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
QDir.homePath())
self.path = osp.dirname(str(fileName))
if fileName != '':
self.mediaPlayer.setMedia(
QMediaContent(QUrl.fromLocalFile(fileName)))
self.playButton.setEnabled(True)
def exitCall(self):
sys.exit(app.exec_())
def screenshotCall(self):
#Call video frame grabber
self.grabber = VideoFrameGrabber(self.videoWidget, self)
self.mediaPlayer.setVideoOutput(self.grabber)
self.mediaPlayer.pause()
self.grabber.frameAvailable.connect(self.process_frame)
self.errorLabel.setText("Taking a screenshot of image "+str(self.counter)+" ....")
self.mediaPlayer.play()
self.mediaPlayer.setVideoOutput(self.videoWidget)
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
def mediaStateChanged(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def positionChanged(self, position):
self.positionSlider.setValue(position)
def durationChanged(self, duration):
self.positionSlider.setRange(0, duration)
def setPosition(self, position):
self.mediaPlayer.setPosition(position)
def handleError(self):
self.playButton.setEnabled(False)
self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())
def process_frame(self, image):
# Save image here
filename = "screenshot" + str(self.counter).zfill(6)
self.path = 'C:/Desktop/temp'
image.save(self.path+'/{}.png'.format(str(filename)))
self.counter = self.counter+1
if __name__ == '__main__':
app = QApplication(sys.argv)
player = VideoWindow()
player.resize(720, 480)
player.show()
sys.exit(app.exec_())
I'm thinking of retrieving the current frame number and creating loops to retrieve the before and after frames and then send them to process_frame for saving, but I'm not sure how to implement that because as of now, I'm still having a hard time grasping the principles behind the video frame grabber.
You have a few options.
First is to seek the video to the frames you need using QMediaPlayer.setPosition, then use the grabber you have already implemented to grab the current frame. Then you'd restore the position.
In reality, this is difficult to implement, since you need to know precise frame times, which is something that PyQT doesn't offer as part of their API.
To obtain frame times, you could use FFMS2 library and its Python bindings:
import ffms
source_file = "test/x264.mkv"
vsource = ffms.VideoSource(source_file)
print("Frames:", vsource.properties.NumFrames)
print("Times:", vsource.track.timecodes)
This is where the other solution kicks in. If you use FFMS2 anyway, why even use the media player to obtain the frames? You can just use FFMS2 directly:
>>> frame = vsource.get_frame(0)
>>> frame.EncodedWidth, frame.EncodedHeight
(128, 72)
>>> frame.planes[0]
array([16, 16, 16, ..., 16, 16, 16], dtype=uint8)
You input the frame number and receive a numpy array that you can convert into an actual image with library such as PIL or scikit-image. So you only need to convert current stream position to the frame number. To do it, you can query the vsource.track.timecodes variable and find the index of the closest frame number. To do it fast, you can use divide-and-conquer technique (the builtin Python bisect module).
Note that you should load the ffms.VideoSource once, when you select the video, since it may take a bit of time to index the time and frame information and you don't want the UI to lag when the user chooses to do a screenshot. To further speed up things, you could also cache the indexing information, if you need to reopen the file often. Manual indexing is covered in FFMS2 documentation.
Related
I'm using PyQt5 to create an app with multiple main windows. I want to be able to allow the user to save and load window sizes and window positions. That's easy with, e.g., QMainWindow.saveGeometry() and QMainWindow.loadGeometry() or the corresponding .saveState() and .loadState() variants. These work great for position and size, but if the user moves or resizes one window so that it occludes another, I want to also restore this positioning. I don't mind writing my own code to save the info for each window, but I can't see any way to detect the relative Z order of windows. Am I missing it in the docs, or is this not possible?
To see what I mean, try this:
from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit
from PyQt5.QtCore import QSettings
from PyQt5.QtGui import QCloseEvent
'''
context: Linux Mint 19.3 Tricia x86_64
Python 3.9
PyQt5 5.15.1
'''
class RememberWin(QMainWindow):
def __init__(self, win_name: str):
super(RememberWin, self).__init__()
self.win_name = win_name
self.setWindowTitle(win_name)
self.can_close = False
def restore_window(self) -> bool:
try:
settings = QSettings("PyQtExamples", "RememberWinTest")
self.restoreGeometry(settings.value(f'{self.win_name} Geometry'))
self.restoreState(settings.value(f'{self.win_name} State'))
return True
except:
return False
def closeEvent(self, event: QCloseEvent):
if not self.can_close:
event.ignore()
else:
settings = QSettings("PyQtExamples", "RememberWinTest")
settings.setValue(f'{self.win_name} Geometry', self.saveGeometry())
settings.setValue(f'{self.win_name} State', self.saveState())
QMainWindow.closeEvent(self, event)
class ControlWindow(RememberWin):
def __init__(self, win_name: str = "ControlWindow"):
super(ControlWindow, self).__init__(win_name=win_name)
self.can_close = True
self.window1 = RememberWin(win_name='WindowOne')
self.window2 = RememberWin(win_name='WindowTwo')
self.text = QPlainTextEdit(self)
s = "Try making Window1 wide enough to cover Window2.\n" \
"Then close this window (auto closes others).\n" \
"Re-run the app and you'll notice that Window2\n" \
"is not on top of Window1 which means that this\n" \
"info isn't getting saved."
self.text.setPlainText(s)
self.setCentralWidget(self.text)
if not self.restore_window():
self.setGeometry(100, 390, 512, 100)
if not self.window1.restore_window():
self.window1.setGeometry(100, 100, 512, 384)
if not self.window2.restore_window():
self.window2.setGeometry(622, 100, 512, 384)
self.window1.show()
self.window2.show()
def closeEvent(self, event: QCloseEvent):
for win in (self.window1, self.window2):
win.can_close = True
win.close()
super(ControlWindow, self).closeEvent(event)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = ControlWindow(win_name='ControlWindow (You can only close this one)')
window.show()
sys.exit(app.exec_())
The simplest way to do what you want to achieve is to keep track of the current focused widget, or, to be precise, the top level window of the last focused widget.
You can store the focused windows in the settings as a list, using a unique objectName for each window (you are already doing this, so you just need to use setObjectName()), then restore the window by showing them in the correct order as long as the object name matches.
class RememberWin(QMainWindow):
def __init__(self, win_name: str):
super(RememberWin, self).__init__()
self.win_name = win_name
self.setObjectName(win_name)
self.setWindowTitle(win_name)
self.can_close = False
# ...
class ControlWindow(RememberWin):
def __init__(self, win_name: str = "ControlWindow"):
# ...
self.settings = QSettings("PyQtExamples", "RememberWinTest")
self.zOrder = []
QApplication.instance().focusObjectChanged.connect(self.focusChanged)
windowOrder = self.settings.value('windowOrder', type='QStringList')
topLevelWindows = QApplication.topLevelWidgets()
if windowOrder:
for objName in windowOrder:
for win in topLevelWindows:
if win.objectName() == objName:
win.show()
else:
self.window1.show()
self.window2.show()
def focusChanged(self, obj):
if not obj or obj.window() == self.window():
return
if obj.window() in self.zOrder[:-1]:
self.zOrder.remove(obj.window())
self.zOrder.append(obj.window())
def closeEvent(self, event: QCloseEvent):
for win in (self.window1, self.window2):
win.can_close = True
win.close()
self.settings.setValue('windowOrder',
[w.window().objectName() for w in self.zOrder])
super(ControlWindow, self).closeEvent(event)
I have the below piece of code which gives the below picture.
import os
import numpy as np
from PyQt5 import QtCore, QtWidgets
import sqlite3
class Ui_Form():
def __init__(self):
#Checking if the loading database is in place
if not os.path.exists("loading_database.db"):
QtWidgets.QMessageBox.information(None,'Loading database missing','Loading database has not been found. Creation of a new one will be attempted')
self.loadingDatabaseCreator()
QtWidgets.QMessageBox.information(None,'Successful','Loading database succesfully created')
#Asking the user for the input file to be parsed and the number of componenets determined
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Select input model","","Input deck (*.inp)","*.inp")
filePath = str(filePath)
self.pleaseWait = waitWindow()
self.pleaseWait.show()
#If no file has been inputted the script will exit
if not filePath:
exit()
else:
#If a file has been inputted now it will be opened and a list containing all the lines will be created
readInputFile(filePath)
#Searching in the file for all the valid components. We disregards collectors containing RBE3 elements
#as they don't require fatigue analysis
self.pleaseWait.close()
for line in model_file:
if "*ELEMENT," in line and "DCOUP3D" not in line:
#If a valid collector is found it will be added to the array of type numpy.
try:
#Checks if the collector has already been recorded as different element types partaining of the same component
#will be specified in different collectors win the input deck
if not line.split("ELSET=")[1][:-1] in self.collector_array:
self.collector_array = np.concatenate((self.collector_array,np.array([line.split("ELSET=")[1][:-1]])),axis=0)
except:
self.collector_array = np.array([line.split("ELSET=")[1][:-1]])
#model_file_obj.close
#Testing to see if the array has been created indicating the presence of at least one entity
#This will be useful if the user loads a load deck instead of a model as they have the same .inp extension
try:
self.collector_array
except:
QtWidgets.QMessageBox.information(None,'Error','File contains no element collectors')
#Creating the initial Window
self.mainWidget = QtWidgets.QWidget()
self.mainWidget.resize(500, 500)
self.mainWidget.setWindowFlags(self.mainWidget.windowFlags() | QtCore.Qt.MSWindowsFixedSizeDialogHint)
self.mainWidget.setWindowTitle("nCode analysis set-up")
#Creating the top level grid layout
self.mainGrid = QtWidgets.QGridLayout(self.mainWidget)
#Creating the boxes which will describe the analysis to be written in the .dcl file
self.analysis_type_label = QtWidgets.QLabel(self.mainWidget)
self.analysis_type_label.setText("Type of analysis")
self.mainGrid.addWidget(self.analysis_type_label,0,0)
self.analysis_type_combo = QtWidgets.QComboBox(self.mainWidget)
self.analysis_type_combo.addItems(["Fatigue","Proof plus fatigue"])
self.mainGrid.addWidget(self.analysis_type_combo,0,1,1,2)
self.load_deck_type_label = QtWidgets.QLabel(self.mainWidget)
self.load_deck_type_label.setText("Type of fatigue deck")
self.mainGrid.addWidget(self.load_deck_type_label,1,0)
self.load_deck_type_combo = QtWidgets.QComboBox(self.mainWidget)
self.load_deck_type_combo.addItems(["Regen braking","No regen braking"])
self.mainGrid.addWidget(self.load_deck_type_combo,1,1,1,2)
self.analysis_engine_type_label = QtWidgets.QLabel(self.mainWidget)
self.analysis_engine_type_label.setText("Analysis Engine")
self.mainGrid.addWidget(self.analysis_engine_type_label,2,0)
self.analysis_engine_type_combo = QtWidgets.QComboBox(self.mainWidget)
self.analysis_engine_type_combo.addItems(["EN analysis","SN analysis"])
self.mainGrid.addWidget(self.analysis_engine_type_combo,2,1,1,2)
#Creating a scrolable area to accommodate for a large number of components with possible lenghty names
self.scrollArea = QtWidgets.QScrollArea(self.mainWidget)
#The line below is absolutely required to make the scrollable area work.
self.scrollArea.setWidgetResizable(True)
self.mainGrid.addWidget(self.scrollArea,3,0,1,3)
self.secondaryWidget = QtWidgets.QWidget()
self.scrollArea.setWidget(self.secondaryWidget)
self.secondaryGrid = QtWidgets.QGridLayout(self.secondaryWidget)
#This bit creates the necessary object for every componenet that was found in the input deck.
#The globals method is used to dynamically assign objects to variables for subsequent manipulation.
for i in range(0, self.collector_array.shape[0]):
globals()["self.materialLabel"+str(i)] = QtWidgets.QLabel(self.secondaryWidget)
globals()["self.materialLabel"+str(i)].setText(self.collector_array[i]+" material")
self.secondaryGrid.addWidget(globals()["self.materialLabel"+str(i)],2+i,0)
globals()["self.materialName"+str(i)] = QtWidgets.QLineEdit(self.secondaryWidget)
globals()["self.materialName"+str(i)].setPlaceholderText("Drop material name here")
globals()["self.materialName"+str(i)].setFixedWidth(150)
self.secondaryGrid.addWidget(globals()["self.materialName"+str(i)],2+i,1)
globals()["self.materialPickingButton"+str(i)] = QtWidgets.QPushButton(self.secondaryWidget)
globals()["self.materialPickingButton"+str(i)].setText("Pick material")
globals()["self.materialPickingButton"+str(i)].clicked.connect(self.material_lookup)
self.secondaryGrid.addWidget(globals()["self.materialPickingButton"+str(i)],2+i,2)
#Creates the button that connects to the DLC_writer function
self.createDCL = QtWidgets.QPushButton(self.mainWidget)
self.createDCL.setText("Create DCL")
self.mainGrid.addWidget(self.createDCL,4,0,1,3)
self.createDCL.clicked.connect(self.DCL_guide)
self.mainWidget.show()
class waitWindow(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Info")
self.resize(600,200)
self.VLayout = QtWidgets.QVBoxLayout(self)
self.message = QtWidgets.QLabel(self)
self.message.setFixedWidth(550)
self.message.setText("Please wait while input file is being read")
self.VLayout.addWidget(self.message)
class readInputFile():
def __init__(self,filePath):
model_file_obj = open(filePath, "r")
globals()['model_file'] = model_file_obj.readlines()
model_file_obj.close
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Ui_Form()
sys.exit(app.exec_())
The problem is my text label is missing from this window. I made it so big in case the label did not have enough space to fully display but in that case I think it would have displayed what it had space for. Hopefully someone knows why.
Edit: I have included the entire init function of Ui_Form. All my problems are caused in this bit the rest working ok.
The window you are viewing is not pleaseWait window but the mainWidget window.
The above is explained assuming that:
The file that is read is small, so the pleaseWait window will open and close instantly, so that being that synchronous action Qt will not have time to do it and for the user the window will never have been shown. For this case the solution is to give a reasonable time for the user to see the window.
The file is very large, the reading will take a long time blocking the eventloop, which will cause tasks such as displaying a window to not be performed, to avoid blocking the task must be executed in another thread.
Combining both solutions we obtain the following code:
import os
from functools import partial
from PyQt5 import QtCore, QtWidgets
class Worker(QtCore.QObject):
finished = QtCore.pyqtSignal()
contentChanged = QtCore.pyqtSignal(list)
#QtCore.pyqtSlot(str)
def read_file(self, fileName):
with open(fileName, "r") as model_file_obj:
model_file = model_file_obj.readlines()
print(model_file)
self.contentChanged.emit(model_file)
self.finished.emit()
class MainWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(500, 500)
self.setWindowFlags(
self.windowFlags() | QtCore.Qt.MSWindowsFixedSizeDialogHint
)
self.setWindowTitle("nCode analysis set-up")
mainGrid = QtWidgets.QGridLayout(self)
thread = QtCore.QThread(self)
thread.start()
self.m_worker = Worker()
self.m_worker.moveToThread(thread)
self.m_worker.contentChanged.connect(self.get_content)
def launch_task(self):
if not os.path.exists("loading_database.db"):
QtWidgets.QMessageBox.information(
None,
"Loading database missing",
"Loading database has not been found. Creation of a new one will be attempted",
)
# self.loadingDatabaseCreator()
QtWidgets.QMessageBox.information(
None, "Successful", "Loading database succesfully created"
)
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None, "Select input model", "", "Input deck (*.inp)", "*.inp"
)
self.pleaseWait = WaitWindow()
self.pleaseWait.show()
self.m_worker.finished.connect(self.pleaseWait.close)
wrapper = partial(self.m_worker.read_file, fileName)
# Launch the task in a reasonable time for the window to show
QtCore.QTimer.singleShot(100, wrapper) #
#QtCore.pyqtSlot(list)
def get_content(self, lines):
print(lines)
class WaitWindow(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Info")
self.resize(600, 200)
layout = QtWidgets.QVBoxLayout(self)
self.message = QtWidgets.QLabel(self)
self.message.setFixedWidth(550)
self.message.setText("Please wait while input file is being read")
layout.addWidget(self.message)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWidget()
w.show()
w.launch_task()
sys.exit(app.exec_())
Update:
import os
from functools import partial
import numpy as np
from PyQt5 import QtCore, QtWidgets
class MainWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(500, 500)
self.setWindowFlags(
self.windowFlags() | QtCore.Qt.MSWindowsFixedSizeDialogHint
)
self.setWindowTitle("nCode analysis set-up")
self.wait_window = WaitWindow()
thread = QtCore.QThread(self)
thread.start()
self.m_worker = Worker()
self.m_worker.moveToThread(thread)
self.m_worker.new_content_signal.connect(self.get_content)
# Creating the top level grid layout
mainGrid = QtWidgets.QGridLayout(self)
self.analysis_type_label = QtWidgets.QLabel(self)
self.analysis_type_label.setText("Type of analysis")
mainGrid.addWidget(self.analysis_type_label, 0, 0)
self.analysis_type_combo = QtWidgets.QComboBox(self)
self.analysis_type_combo.addItems(["Fatigue", "Proof plus fatigue"])
mainGrid.addWidget(self.analysis_type_combo, 0, 1, 1, 2)
self.load_deck_type_label = QtWidgets.QLabel(self)
self.load_deck_type_label.setText("Type of fatigue deck")
mainGrid.addWidget(self.load_deck_type_label, 1, 0)
self.load_deck_type_combo = QtWidgets.QComboBox(self)
self.load_deck_type_combo.addItems(
["Regen braking", "No regen braking"]
)
mainGrid.addWidget(self.load_deck_type_combo, 1, 1, 1, 2)
self.analysis_engine_type_label = QtWidgets.QLabel(self)
self.analysis_engine_type_label.setText("Analysis Engine")
mainGrid.addWidget(self.analysis_engine_type_label, 2, 0)
self.analysis_engine_type_combo = QtWidgets.QComboBox(self)
self.analysis_engine_type_combo.addItems(["EN analysis", "SN analysis"])
mainGrid.addWidget(self.analysis_engine_type_combo, 2, 1, 1, 2)
# Creating a scrolable area to accommodate for a large number of components with possible lenghty names
self.scrollArea = QtWidgets.QScrollArea(self)
# The line below is absolutely required to make the scrollable area work.
self.scrollArea.setWidgetResizable(True)
mainGrid.addWidget(self.scrollArea, 3, 0, 1, 3)
self.secondaryWidget = QtWidgets.QWidget()
self.scrollArea.setWidget(self.secondaryWidget)
self.secondaryGrid = QtWidgets.QGridLayout(self.secondaryWidget)
self.createDCL = QtWidgets.QPushButton(self)
self.createDCL.setText("Create DCL")
mainGrid.addWidget(self.createDCL, 4, 0, 1, 3)
def start_task(self):
if not os.path.exists("loading_database.db"):
QtWidgets.QMessageBox.information(
None,
"Loading database missing",
"Loading database has not been found. Creation of a new one will be attempted",
)
# self.loadingDatabaseCreator()
QtWidgets.QMessageBox.information(
None, "Successful", "Loading database succesfully created"
)
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(
None, "Select input model", "", "Input deck (*.inp)", "*.inp"
)
if filePath:
self.wait_window.show()
self.m_worker.finished.connect(self.wait_window.close)
wrapper = partial(self.m_worker.read_file, filePath)
# Launch the task in a reasonable time for the window to show
QtCore.QTimer.singleShot(100, wrapper)
#QtCore.pyqtSlot(int, str)
def get_content(self, i, content):
label = QtWidgets.QLabel("{} material".format(content))
linedit = QtWidgets.QLineEdit(placeholderText="Drop material name here")
linedit.setFixedWidth(150)
button = QtWidgets.QPushButton("Pick material")
self.secondaryGrid.addWidget(label, 2 + i, 0)
self.secondaryGrid.addWidget(linedit, 2 + i, 1)
self.secondaryGrid.addWidget(button, 2 + i, 2)
class WaitWindow(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Info")
self.resize(600, 200)
layout = QtWidgets.QVBoxLayout(self)
self.message = QtWidgets.QLabel()
self.message.setFixedWidth(550)
self.message.setText("Please wait while input file is being read")
layout.addWidget(self.message)
class Worker(QtCore.QObject):
finished = QtCore.pyqtSignal()
new_content_signal = QtCore.pyqtSignal(int, str)
#QtCore.pyqtSlot(str)
def read_file(self, fileName):
i = 0
collector_array = []
with open(fileName, "r") as model_file_obj:
for line in model_file_obj.readlines():
if "*ELEMENT," in line and "DCOUP3D" not in line:
t = line.split("ELSET=")[1][:-1]
if t not in collector_array:
self.new_content_signal.emit(i, t)
QtCore.QThread.msleep(10)
collector_array.append(t)
i += 1
self.finished.emit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWidget()
w.show()
w.start_task()
sys.exit(app.exec_())
This code works perfectly for me:
import sys
from PyQt5 import QtWidgets
from PyQt5.Qt import QApplication
class waitWindow(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Info")
self.resize(600,200)
self.VLayout = QtWidgets.QVBoxLayout(self)
self.message = QtWidgets.QLabel(self)
self.message.setFixedWidth(550)
self.message.setText("Please wait while input file is being read")
self.VLayout.addWidget(self.message)
self.show()
def closeWindow(self):
self.close()
app = QApplication(sys.argv)
w = waitWindow()
w.exec_()
Okay took your code and gutted all the unessential stuff to analyze the actual issue I have included the gutted version so you can see what is necessary in the future to see what is wrong.
In short the issue is that you need to add the following line as the last line in your waitWindow function >> self.exec() by adding that I got your label to display (see code below)
Now with that said you do have another issue and that is the QDialog box will not allow the program to continue until it has been closed (aka it stops the process flow until you release it by closing the window or use a different means). My question is why not use your "main window" to display that message then repopulate with the rest of that data. Also curious why are you not using QMainWindow?
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui_Form():
def __init__(self):
self.pleaseWait = waitWindow()
self.pleaseWait.show()
self.pleaseWait.close()
sys.exit()
class waitWindow(QDialog):
def __init__(self):
super(waitWindow, self).__init__()
self.setWindowTitle("Info")
self.resize(600,200)
self.message = QLabel(self)
self.message.setFixedWidth(550)
self.message.setText("Please wait while input file is being read")
self.VLayout = QVBoxLayout(self)
self.VLayout.addWidget(self.message)
self.setLayout(self.VLayout)
self.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = Ui_Form()
sys.exit(app.exec_())
I have a mainwindow which has a button which opens a second window when clicked. This second window contains a mayavi plot. Every time I click this button an error is shown
QCoreApplication::exec: The event loop is already running
I have looked at multiple similar questions on SO, but I cannot fix my problem.
The code of the mainwindow maingui.py:
# Standard library imports
import re
import sys
import string
import inspect
import platform
import importlib
import ctypes
from os.path import abspath, join, dirname, realpath
from functools import wraps
import inspect
import matplotlib as mpl
# Local imports
from loadhistory.stresscorrection import DnvThicknessStressCorrection
# PyQt5 imports
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QListView, QWidget, QGridLayout
from PyQt5.QtGui import QPixmap
# Gui package import
import gui.design
import gui.resources
from gui.inputs import Inputs
from gui.filemanager import Filemanager
from gui.datawrapper import RunThread
from gui.mayaviwindow import MayaviWindow
class MainWindow(QMainWindow, gui.design.Ui_MainWindow):
'''main window of the gui'''
def __init__(self):
QMainWindow.__init__(self)
super().__init__()
# Bypass needed to set taskbar icon if on windows
if "Windows" in platform.system():
myappid = 'LaboSoete'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
self.inputs = Inputs(self)
self.filemanager = Filemanager(self)
self.mayaviwindow = MayaviWindow()
self.setupUi(self)
self.setWindowTitle("Endurance framework SafeLife")
# Apply css stylesheet
sshFile = "gui/stylesheet.css"
with open(sshFile, "r") as fh:
self.setStyleSheet(fh.read())
# Initialize combobox design codes
self.designcodes = self.inputs.code_dict()
codes = list(self.designcodes.keys())
self.design_code_combo.addItems(codes)
self.design_cat_combo.addItems(self.designcodes[codes[0]])
self.scrollbar_QComboBox(self.design_code_combo)
self.scrollbar_QComboBox(self.design_cat_combo)
# Initialize combobox Damagemodels
module = importlib.import_module('damagemodels.damagemodels')
for m in module.__dict__.values():
if inspect.isclass(m):
if 'damagemodels.damagemodels.' in str(m):
if not inspect.isabstract(m):
self.dmgBox.addItem(m.__name__)
###### Menubar ######
self.actionSave.triggered.connect(self.filemanager.save)
self.actionSave_as.triggered.connect(self.filemanager.save_as)
self.actionLoad.triggered.connect(self.filemanager.load)
self.actionOutput.triggered.connect(self.filemanager.change_output_dir)
self.actionQuit.triggered.connect(self.close)
###### Button handeling ######
self.loadhistory_btn.clicked.connect(self.inputs.lh_browse_file)
self.odb_btn.clicked.connect(self.inputs.odb_browse_file)
self.design_code_combo.currentIndexChanged.connect(
self.inputs.design_combo_currentIndexChanged)
self.btn_snCustom.clicked.connect(self.inputs.sn_browse_file)
self.run_btn.clicked.connect(self.run_analysis)
self.newBtn.clicked.connect(self.filemanager.new)
self.abaqus_results_btn.clicked.connect(self.filemanager.open_odb_results)
self.damageNnumber.textChanged.connect(lambda: self.check_numeric_entry(self.damageNnumber))
self.hss_visualization_btn.clicked.connect(self.mayaviwindow.open_new_window)
# DNV thickness correction initialization
self.thicknessFrame.hide()
self.design_cat_combo.currentIndexChanged.connect(self.show_thickness_correction)
self.loadhistory_txt.textChanged.connect(self.loadhistory_txt_changed)
self.radioBtn_snBasquin.toggled.connect(self.show_thickness_correction)
self.radioBtn_snCustom.toggled.connect(self.show_thickness_correction)
self.radioBtn_snDesign.toggled.connect(self.show_thickness_correction)
self.radioBtn_snLog.toggled.connect(self.show_thickness_correction)
###### Tab/plot handeling ######
# Loadhistory plot
self.tabWidget.setCurrentIndex(0)
self.loadhistory_txt.textChanged.connect(lambda: self.LhWidget.run(self))
self.thicknessCorrCheckbox.toggled.connect(lambda: self.LhWidget.run(self))
self.t_txt.textChanged.connect(lambda: self.LhWidget.run(self))
self.tref_txt.textChanged.connect(lambda: self.LhWidget.run(self))
self.k_txt.textChanged.connect(lambda: self.LhWidget.run(self))
self.mscCheckBox.toggled.connect(lambda: self.LhWidget.run(self))
self.mscSelectionBox.currentIndexChanged.connect(lambda: self.LhWidget.run(self))
# Sncurve plot
self.design_cat_combo.currentIndexChanged.connect(self.inputs.design_cat_combo_change)
self.design_code_combo.currentIndexChanged.connect(self.inputs.design_combo_currentIndexChanged)
self.sncurve_txt.textChanged.connect(lambda: self.SnWidget.run(self))
self.radioBtn_snCustom.clicked.connect(lambda: self.SnWidget.run(self))
self.radioBtn_snDesign.clicked.connect(lambda: self.SnWidget.run(self))
self.radioBtn_snBasquin.clicked.connect(self.clear_sntxt_fields)
self.radioBtn_snLog.clicked.connect(self.clear_sntxt_fields)
self.Nf_txt.textChanged.connect(lambda: self.SnWidget.run(self))
self.uts_txt.textChanged.connect(lambda: self.SnWidget.run(self))
self.m1_txt.textChanged.connect(lambda: self.SnWidget.run(self))
self.B1_txt.textChanged.connect(lambda: self.SnWidget.run(self))
self.m2_txt.textChanged.connect(lambda: self.SnWidget.run(self))
self.B2_txt.textChanged.connect(lambda: self.SnWidget.run(self))
# HSS handeling
self.hss_checkbox.toggled.connect(self.show_hss_image)
self.hss_combobox.currentIndexChanged.connect(self.show_hss_image)
self.hss_combobox.currentIndexChanged.connect(self.show_hide_hss_spinbox)
###### Output related ######
self.hss_visualization_btn.hide()
self.hideTab(3)
self.abaqus_results_btn.hide()
self.newBtn.hide()
self.progressBar.hide()
#staticmethod
def eval_eq(equation):
'''
Check if the string 'equation' only contains allowable characters
'''
regex = re.compile(r'(\d+([.]|\*))?\d+(e|[.]|\*)?\d*(e|[.]|\*)?\d*?$')
if (equation.endswith('e') or equation.endswith('.')) and re.match(regex, equation):
return eval(equation[:-1])
elif re.match(regex, equation) and equation.count('e') < 2:
return eval(equation)
else:
raise ValueError(
'Illegal expression used to define the S-N curve, only e^*'+string.digits+' allowed')
def check_numeric_entry(self, myQlineEdit):
'''
For QlineEdit which can only contain numbers, check whether
no non-numeric characters are entered
'''
if myQlineEdit.text().isdigit() or len(myQlineEdit.text()) < 1:
return
else:
reply = QMessageBox.warning(self, 'TypeError', 'This field only allows for numeric entries')
if reply == QMessageBox.Ok:
myQlineEdit.setText(myQlineEdit.text()[:-1])
return
def loadhistory_txt_changed(self):
'''
If a loadhistory is selected the meanstresscorrection option
becomes enabled
'''
if len(self.loadhistory_txt.text())>0:
self.mscCheckBox.setEnabled(True)
else:
self.mscCheckBox.setDisabled(True)
def show_thickness_correction(self):
'''
Determine whether or not the thickness_correction option should be shown and enabled.
This is only the case when a DNV SN curve is selected
'''
if 'DNV' in self.design_code_combo.currentText() and self.radioBtn_snDesign.isChecked() and len(self.loadhistory_txt.text())>0:
self.thicknessFrame.show()
self.thicknessCorrCheckbox.setEnabled(True)
else:
self.thicknessFrame.hide()
self.thicknessCorrCheckbox.setChecked(False)
self.thicknessCorrCheckbox.setDisabled(True)
def show_hss_image(self):
if self.hss_checkbox.isChecked():
filename = self.hss_combobox.currentText()
filepath = abspath(join(dirname(realpath(__file__)), 'gui', 'icons', filename))
pixmap = QPixmap(filepath)
self.hss_imagelabel.setPixmap(pixmap)
self.hss_imagelabel.show()
self.tabWidget.setCurrentIndex(2)
if not self.hss_checkbox.isChecked():
self.hss_imagelabel.hide()
def get_thickness_correction(self):
'''
Return a thickness correction object using the values from the
gui to initialize the object
'''
if "DNV" in self.design_code_combo.currentText():
tref = self.eval_eq(self.tref_txt.text())
t = self.eval_eq(self.t_txt.text())
k = self.eval_eq(self.k_txt.text())
if k >= 1:
reply = QMessageBox.warning(
self, 'ValueError', 'k needs to be less than one, please consult DNV-GL-RP203 for the correct values')
if reply == QMessageBox.Ok or reply == QMessageBox.Cancel:
self.k_txt.clear()
return
else: return
return DnvThicknessStressCorrection(tref, t, k)
def show_hide_hss_spinbox(self):
'''
hide the hss_spinbox and hss_t_label widgets if a hss method, that does not
use the plate thickness to determine the extrapolation coords, is selected
'''
test = self.hss_combobox.currentText()
lst = ['1', '2', '3']
if any(x in test for x in lst):
self.hss_spinbox.show()
self.hss_t_label.show()
else:
self.hss_t_label.hide()
self.hss_t_label.show()
def scrollbar_QComboBox(self, combo):
'''
Add a scrollbar to QComboBoxes if the selected text is wider than
the combobox
'''
view = QListView(combo)
combo.setView(view)
view.setTextElideMode(Qt.ElideNone)
view.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
def closeEvent(self, event):
'''
Handle the event of the user wanting to close the application
'''
msgBox = QMessageBox()
msgBox.setWindowTitle('Exit application?')
msgBox.setText('Do you want to exit the application?')
exit_btn = msgBox.addButton('Exit', QMessageBox.YesRole)
save_exit_btn = msgBox.addButton('Save and Exit', QMessageBox.YesRole)
cancel_btn = msgBox.addButton('Cancel', QMessageBox.RejectRole)
msgBox.setEscapeButton(cancel_btn)
msgBox.exec_()
if msgBox.clickedButton() == exit_btn:
event.accept()
elif msgBox.clickedButton() == save_exit_btn:
self.filemanager.save()
event.accept()
elif msgBox.clickedButton() == cancel_btn:
event.ignore()
def start_thread(self, thread):
'''Pass all MainWindow attributes to the new thread and start the thread'''
thread.progressBar = self.progressBar
thread.loadhistory_txt = self.loadhistory_txt
thread.loadhistory_btn = self.loadhistory_btn
thread.odb_txt = self.odb_txt
thread.odb_btn = self.odb_btn
thread.dmgBox = self.dmgBox
thread.dmax_box = self.dmax_box
thread.radioBtn_snCustom = self.radioBtn_snCustom
thread.radioBtn_snBasquin = self.radioBtn_snBasquin
thread.radioBtn_snDesign = self.radioBtn_snDesign
thread.sncurve_txt = self.sncurve_txt
thread.btn_snCustom = self.btn_snCustom
thread.design_cat_combo = self.design_cat_combo
thread.design_code_combo = self.design_code_combo
thread.mscCheckBox = self.mscCheckBox
thread.mscSelectionBox = self.mscSelectionBox
thread.calcLifetimeBox = self.calcLifetimeBox
thread.calcLifetimeCheckBox = self.calcLifetimeCheckBox
thread.damageNcheckBox = self.damageNcheckBox
thread.damageNnumber = self.damageNnumber
thread.newBtn = self.newBtn
thread.m1_txt = self.m1_txt
thread.B1_txt = self.B1_txt
thread.m2_txt = self.m2_txt
thread.B2_txt = self.B2_txt
thread.uts_txt = self.uts_txt
thread.Nf_txt = self.Nf_txt
thread.SnWidget = self.SnWidget
thread.LhWidget = self.LhWidget
thread.Inputs = self.inputs
thread.thicknessCorrCheckbox = self.thicknessCorrCheckbox
thread.k_txt = self.k_txt
thread.t_txt = self.t_txt
thread.tref_txt = self.tref_txt
thread.hss_spinbox = self.hss_spinbox
thread.hss_combobox = self.hss_combobox
thread.hss_checkbox = self.hss_combobox
thread.start()
def clear_sntxt_fields(self):
'''Clear all text fields related to SN curve equations'''
self.Nf_txt.clear()
self.uts_txt.clear()
self.m1_txt.clear()
self.m2_txt.clear()
self.B1_txt.clear()
self.B2_txt.clear()
def initialize(self):
self.hss_visualization_btn.hide()
self.run_btn.setDisabled(True)
self.newBtn.hide()
self.abaqus_results_btn.hide()
self.hideTab(3)
self.output_txt.clear()
def finished(self):
if self.hss_checkbox.isChecked():
self.hss_visualization_btn.show()
self.run_btn.setEnabled(True)
self.newBtn.show()
self.abaqus_results_btn.show()
self.hide_progressbar()
self.showTab(3)
def show_progressbar(self):
self.progressBar.show()
def hide_progressbar(self):
self.progressBar.hide()
def clear_lhtxt(self):
self.loadhistory_txt.clear()
self.loadhistory_txt.setFocus()
def clear_odbtxt(self):
self.odb_txt.clear()
self.odb_txt.setFocus()
def clear_sncustomtxt(self):
self.sncurve_txt.clear()
self.sncurve_txt.setFocus()
def show_messagebox(self, errortype, msg, methodname):
reply = QMessageBox.warning(self, errortype, msg)
if methodname is not "None":
handle_error = getattr(self, methodname)
if reply == QMessageBox.Ok:
if 'handle_error' in locals():
handle_error()
return
def dmg_curve_plot(self, Dplot, nNplot):
self.DmgWidget.run(self, Dplot, nNplot)
def hideTab(self, tabIndex):
self.tabWidget.setTabEnabled(tabIndex, False)
def showTab(self, tabIndex):
self.tabWidget.setTabEnabled(tabIndex, True)
def check_required_inputs(self):
if "".__eq__(self.loadhistory_txt.text()) or "".__eq__(self.odb_txt.text()) \
or not self.odb_txt.text().endswith('odb'):
return False
if self.radioBtn_snCustom.isChecked() and "".__eq__(self.sncurve_txt.text()):
return False
if self.radioBtn_snBasquin.isChecked() or self.radioBtn_snLog.isChecked():
if ("".__eq__(self.m1_txt.text()) or "".__eq__(self.Nf_txt.text()) or \
"".__eq__(self.uts_txt.text()) or "".__eq__(self.B1_txt.text())):
return False
return True
def run_analysis(self):
mpl.pyplot.close(fig="all")
if self.check_required_inputs() is False:
self.show_messagebox('IOError', 'Please check all required fields', "None")
return
self.run_thread = RunThread(self)
self.run_thread.initialize.connect(self.initialize)
self.run_thread.clear_lhtxt.connect(self.clear_lhtxt)
self.run_thread.clear_odbtxt.connect(self.clear_odbtxt)
self.run_thread.clear_odbtxt.connect(self.clear_sncustomtxt)
self.run_thread.show_messagebox.connect(self.show_messagebox)
self.run_thread.show_progressbar.connect(self.show_progressbar)
self.run_thread.hide_progressbar.connect(self.hide_progressbar)
self.run_thread.finished.connect(self.finished)
self.run_thread.output.connect(self.filemanager.to_output)
self.run_thread.dmg_curve_plot.connect(self.dmg_curve_plot)
self.start_thread(self.run_thread)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
The code of the second window mayaviwindow.py:
import os
import sys
import mayavi.mlab as mlab
os.environ['ETS_TOOLKIT'] = 'qt4'
from pyface.qt import QtGui, QtCore
from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
class MayaviWindow(QtGui.QMainWindow):
def open_new_window(self):
container = QtGui.QWidget()
container.setWindowTitle("Hot spot stress")
layout = QtGui.QGridLayout(container)
mayavi_widget = MayaviQWidget(container)
layout.addWidget(mayavi_widget, 1, 1)
container.show()
self.window = QtGui.QMainWindow()
self.window.setCentralWidget(container)
self.window.show()
class Visualization(HasTraits):
scene = Instance(MlabSceneModel, ())
#on_trait_change('scene.activated')
def update_plot(self):
self.scene.mlab.test_points3d()
# the layout of the dialog screated
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=250, width=300, show_label=False),
resizable=True # We need this to resize with the parent widget
)
class MayaviQWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
layout = QtGui.QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
self.visualization = Visualization()
# The edit_traits call will generate the widget to embed.
self.ui = self.visualization.edit_traits(parent=self,
kind='subpanel').control
layout.addWidget(self.ui)
self.ui.setParent(self)
Could someone explain to me what the problem is?
Edit
changing the class MayaviWindow to inherit from QWidget instead of QMainwindow did not solve the problem
class MayaviWindow(QtGui.QWidget):
def open_new_window(self):
self.container = QtGui.QWidget()
self.container.setWindowTitle("Hot spot stress")
layout = QtGui.QGridLayout(self.container)
mayavi_widget = MayaviQWidget(self.container)
layout.addWidget(mayavi_widget, 1, 1)
self.container.show()
The code below just displays a tree view of computer drives. Each time a new file/folder is selected, the view scrolls to make this new selection visible.
Question 1: While this works, the initial selection after the application is launched doesn't trigger the scroll. Why?
Question 2: If the instructions:
self.my_view.scrollTo(index, QAbstractItemView.EnsureVisible)
self.my_view.resizeColumnToContents(0)
are inverted:
self.my_view.resizeColumnToContents(0)
self.my_view.scrollTo(index, QAbstractItemView.EnsureVisible)
the first column size is not adjusted either on the initial display, only after. Why?
import sys
from PyQt5.QtCore import Qt, QModelIndex, QDir
from PyQt5.QtWidgets import QApplication, QTreeView, QMainWindow, QFileSystemModel, QAbstractItemView
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
# Instance variables
self.my_view = QTreeView()
self.my_model = QFileSystemModel()
# Init FS model to show all computer drives
model_root_path = str(self.my_model.myComputer())
self.my_model.setRootPath(model_root_path)
# Init tree view
self.my_view.setModel(self.my_model)
self.my_view.setRootIndex(self.my_model.index(model_root_path))
self.my_view.setSelectionMode(QAbstractItemView.SingleSelection)
self.my_view.setSelectionBehavior(QAbstractItemView.SelectRows)
# Connect selection change events to custom slot
select_model = self.my_view.selectionModel()
select_model.currentRowChanged.connect(self.current_row_changed)
# Main window
self.setCentralWidget(self.my_view)
self.setGeometry(200, 200, 800, 600)
# Select initial row on view
focus_path = QDir.currentPath()
focus_index = self.my_model.index(focus_path)
self.my_view.setCurrentIndex(focus_index)
def current_row_changed(self):
"""Current row of the model has changed"""
# Scroll view to new row
index = self.my_view.selectionModel().currentIndex()
self.my_view.scrollTo(index, QAbstractItemView.EnsureVisible)
self.my_view.resizeColumnToContents(0)
# Show path of current row in window title
absolute_path = self.my_model.filePath(index)
self.setWindowTitle(absolute_path)
def main():
a = QApplication(sys.argv)
mw = MyWindow()
mw.show()
sys.exit(a.exec_())
if __name__ == '__main__':
main()
`
Edit: After using the good solution provided by #ekhumoro, my sample code above worked. However this other piece of code still didn't:
import os
import sys
from PyQt5.QtCore import pyqtSignal, QTimer, QDir, Qt
from PyQt5.QtWidgets import QMainWindow, QGridLayout, QWidget, QTreeView, QAbstractItemView, QFileSystemModel, \
QApplication
class AppWindow(QMainWindow):
default_folder_path = "."
def __init__(self):
super().__init__()
self.folder_view = FolderTreeView()
self.folder_view.folder_has_changed.connect(self.folder_changed)
self.build_ui()
self.show()
# Select initial folder
self.select_initial_folder()
def build_ui(self):
main_widget = QWidget()
layout = QGridLayout(main_widget)
layout.addWidget(self.folder_view)
self.setCentralWidget(main_widget)
self.setGeometry(200, 100, 800, 600)
def select_initial_folder(self):
folder_index = self.folder_view.get_index(AppWindow.default_folder_path)
if folder_index.isValid():
self.folder_view.select_folder(folder_index)
def folder_changed(self, folder_path):
if not os.path.isdir(folder_path):
print("Non existing folder:", folder_path)
return
class FolderTreeView(QTreeView):
folder_has_changed = pyqtSignal(str)
def __init__(self):
super().__init__()
self.folder_tree_model = FolderTreeModel()
self.setModel(self.folder_tree_model)
self.setSelectionMode(QAbstractItemView.SingleSelection)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
def select_folder(self, folder_index):
self.setCurrentIndex(folder_index)
def currentChanged(self, current, previous):
super(FolderTreeView, self).currentChanged(current, previous)
# Scroll the view to current item and resize folder name column
QTimer.singleShot(50, lambda: self.delayed_scroll(current))
# Emit signal for other uses
self.folder_has_changed.emit(self.folder_tree_model.filePath(current))
def delayed_scroll(self, index):
self.scrollTo(index, QAbstractItemView.EnsureVisible)
self.resizeColumnToContents(0)
def get_index(self, folder_path):
return self.folder_tree_model.index(folder_path)
class FolderTreeModel(QFileSystemModel):
def __init__(self):
super().__init__()
self.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
self.setRootPath("")
def main():
app = QApplication(sys.argv)
window = AppWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The first problem may be caused if, by default, the model initialises its current index to the current directory. This would mean that if you set it again to the same index, the row-change signal will not be emitted (because nothing changed). This can be fixed by calling the row-change handler directly:
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
...
focus_path = QDir.currentPath()
focus_index = self.my_model.index(focus_path)
self.my_view.setCurrentIndex(focus_index)
self.current_row_changed()
def current_row_changed(self):
index = self.my_view.currentIndex()
self.my_view.scrollTo(index, QAbstractItemView.EnsureVisible)
self.my_view.resizeColumnToContents(0)
...
As to the second problem: when you call scrollTo, it may have to expand several directories in order to select the required index. This could obviously change the width of the first column, so you should always call resizeColumnToContents afterwards in order to get the correct width.
UPDATE:
I think there is also another problem caused by timing issues. The QFileSystemModel must work asynchronously to some extent, because it has to request resources from the operating system and then wait for the response. Also, before it gets the response, it cannot know in advance exactly how much data it is going to receive, because the file-system may have been updated while it was waiting. Potentially, the response could include data from a huge directory containing thousands of files. So in order to keep the GUI responsive, the data is processed in batches which are of a sufficient size to fill the current view. If the current index is set before the window has been shown and all its widgets fully laid out, there is no guarantee that the view will be able to resize its columns correctly.
This can be fixed by explicitly re-calling the row-change handler via a one-shot timer with a small delay. This should allow the view to recalculate its column widths correctly:
...
focus_path = QDir.currentPath()
focus_index = self.my_model.index(focus_path)
self.my_view.setCurrentIndex(focus_index)
QTimer.singleShot(50, self.current_row_changed)
def current_row_changed(self):
index = self.my_view.currentIndex()
self.my_view.scrollTo(index, QAbstractItemView.EnsureVisible)
self.my_view.resizeColumnToContents(0)
Background: I am attempting to make a calendar app. I´m creating a custom view of the month by arranging QGraphicsItemGroup elements for each date in the given month.All QGraphicsItemGroup elements are in a list for easy iteration. While the mouse hovers over a date, the background changes to grey. When a date is clicked, the background changes to blue. When another date is clicked it is marked blue and the previous date is cleared.
Month overview
Question: I just want the last clicked date to have a blue background, not all clicked dates. All the other date elements should have a white background. I can´t figure out how to send a signal from one QGraphicsItemGroup element in the date list to all elements in that list.
Current Code:
# coding=utf-8
from PyQt4.QtCore import Qt
import sys
from datetime import *
from calendar import Calendar
from PyQt4.QtGui import (QApplication, QDialog, QGraphicsView, QGraphicsScene, QGraphicsRectItem, QVBoxLayout,
QGraphicsSimpleTextItem, QBrush, QFont, QGraphicsItemGroup)
class GraficsView(QDialog):
def __init__(self, parent=None):
monthnames = ("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "November",
"Dezember")
super(GraficsView, self).__init__(parent)
self.viewbox = QGraphicsView()
layout = QVBoxLayout()
layout.addWidget(self.viewbox)
self.setLayout(layout)
self.scene = QGraphicsScene(self.viewbox)
self.scene.setSceneRect(0,0,701,501)
self.calendarList = CalendarList(datetime(2018,2,1), self.scene)
self.viewbox.setScene(self.scene)
class DateElement(QGraphicsItemGroup):
def __init__(self, scene, status=False, date=datetime.today(), coordinates=()):
QGraphicsItemGroup.__init__(self, scene=scene)
wochentage = ("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag")
self.status = status
if self.status:
self.setdate(date)
self.setcoordinates(coordinates)
def setdate(self, date):
wochentage = ("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag")
self.date = date
self.dayname = wochentage[self.date.weekday()]
self.daynumber = self.date.day
def setcoordinates(self, coordinates):
self.coordinates = coordinates
self.rectangle = QGraphicsRectItem(self.coordinates[0], self.coordinates[1], self.coordinates[2],
self.coordinates[3])
self.rectangle.setAcceptHoverEvents(True)
self.fontA = QFont("Verdana", 10, QFont.Bold)
self.fontB = QFont("Verdana", 18, QFont.Bold)
self.lableday = QGraphicsSimpleTextItem()
if self.dayname in ("Samstag", "Sonntag"):
self.lableday.setBrush(QBrush(Qt.red))
else:
self.lableday.setBrush(QBrush(Qt.black))
self.lableday.setFont(self.fontA)
self.lableday.setText(self.dayname)
self.lableday.setPos(self.coordinates[0]+3, self.coordinates[1])
self.lablenumber = QGraphicsSimpleTextItem()
if self.dayname in ("Samstag", "Sonntag"):
self.lablenumber.setBrush(QBrush(Qt.red))
else:
self.lablenumber.setBrush(QBrush(Qt.black))
self.lablenumber.setFont(self.fontB)
self.lablenumber.setText(str(self.daynumber)+".")
self.lablenumber.setPos(self.coordinates[0]+3, self.coordinates[1]+12)
self.addToGroup(self.rectangle)
self.addToGroup(self.lableday)
self.addToGroup(self.lablenumber)
def hoverEnterEvent(self, event):
self.rectangle.setBrush(QBrush(Qt.lightGray))
self.update()
def hoverLeaveEvent(self, event):
self.rectangle.setBrush(QBrush(Qt.white))
self.update()
def mousePressEvent(self, event):
self.rectangle.setAcceptHoverEvents(False)
self.rectangle.setBrush(QBrush(Qt.blue))
self.update()
def clearRectangle(self):
self.rectangle.setAcceptHoverEvents(True)
self.rectangle.setBrush(QBrush(Qt.white))
self.update()
class CalendarList():
def __init__(self, date, scene):
self.date = date
self.scene = scene
self.dateelements = []
self.createdates()
def createdates(self):
weekcount = 0
daycount = 0
calendarobjekt = Calendar()
for dateobjekt in calendarobjekt.itermonthdates(self.date.year, self.date.month):
if daycount < 7:
if dateobjekt.month == self.date.month:
self.dateelements.append(DateElement(self.scene, True, dateobjekt, (daycount*100, weekcount*100, 100, 100)))
else:
self.dateelements.append(DateElement(self.scene))
daycount += 1
else:
daycount = 0
weekcount += 1
if dateobjekt.month == self.date.month:
self.dateelements.append(DateElement(self.scene, True, dateobjekt, (daycount*100, weekcount*100, 100, 100)))
else:
self.dateelements.append(DateElement(self.scene))
daycount += 1
app = QApplication(sys.argv)
form = GraficsView()
form.show()
app.exec_()
Possible Solutions:
Create a signal in the CalendarList class, that can be raised by a DateElement, which then triggers the clearBackground methode of all items in the CalendarList.
CalendarList is not a class that inherits from QObject so it does not support the signals, my answer goes in the sense of obtaining the items through scene().items(), then we just filter the items of the DateElement class and have a true status, then the clearRectangle() method will be used:
def mousePressEvent(self, event):
for item in self.scene().items():
if isinstance(item, DateElement):
if item.status:
item.clearRectangle()
self.rectangle.setAcceptHoverEvents(False)
self.rectangle.setBrush(QBrush(Qt.blue))
#self.update()