In my PyQt4 application, there is a functionality that allows users to save a avi file.
To this aim, a saveMovie method has been implemented in the main window:
def saveMovie(self):
""" Let the user make a movie out of the current experiment. """
filename = QtGui.QFileDialog.getSaveFileName(self, "Export Movie", "",
'AVI Movie File (*.avi)')
if filename != "":
dialog = QtGui.QProgressDialog('',
QtCore.QString(),
0, 100,
self,
QtCore.Qt.Dialog |
QtCore.Qt.WindowTitleHint)
dialog.setWindowModality(QtCore.Qt.WindowModal)
dialog.setWindowTitle('Exporting Movie')
dialog.setLabelText('Resampling...')
dialog.show()
make_movie(self.appStatus, filename, dialog)
dialog.close()
My idea is to use a QProgressDialog to show how the video encoding work is proceeding.
Nevertheless, after the selection of the filename, the QFileDialog won't disappear and the entire application stays unresponsive until the make_movie function has completed.
What should I do to avoid this?
Lesson learned: if you have some long-running operations to do -- for example, reading or writing a big file, move them to another thread or they will freeze the UI.
Therefore, I created a subclass of QThread, MovieMaker, whose run method encapsulates the functionality previosly implemented by make_movie:
class MovieMaker(QThread):
def __init__(self, uAppStatus, uFilename):
QtCore.QThread.__init__(self, parent=None)
self.appStatus = uAppStatus
self.filename = uFilename
def run(self):
## make the movie and save it on file
Let's move back to the saveMovie method. Here, I replaced the original call to make_movie with the following code:
self.mm = MovieMaker(self.appStatus,
filename)
self.connect(self.mm, QtCore.SIGNAL("Progress(int)"),
self.updateProgressDialog)
self.mm.start()
Note how I defined a new signal, Progress(int).
Such a signal is emitted by the MovieMaker thread to update the QProgressDialog used to show the user how the movie encoding work is progressing.
Related
I have created a UI with PyQt5. I can use it on Windows and it works perfectly, but when I try to use it on MacOS I get stuck trying to close it (with self.close()). Using the PyCharm debugger I found out that after self.close() it jumps to app.exec_() and the function that was entered to close it is executed again (for example on_later_button_clicked(self)). I have also already tried sys.exit(app.exec_()).
Here is my code:
import os
import sys
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi
from Modules.database import addNeverID
from Modules.supportedWebsites import getWebsites
def Start():
m = askForPartnerUrl()
# m.setFixedSize(500,500)
m.show()
return m
class askForPartnerUrl(QDialog):
def __init__(self):
super(askForPartnerUrl, self).__init__()
loadUi('lib/askForPartnerURL.ui', self)
self.setWindowTitle('Upload')
current_id = getFromFile("id.txt")
self.show_id.setText(current_id)
self.show_origin_url.setText(
'' + getFromFile("origin_url.txt") + '')
self.show_origin_url.setOpenExternalLinks(True)
id_beginns = ["1"]
website_eq = ["1"]
website_guess_str = "Nicht verfügbar!"
for i in range(len(id_beginns)):
if id_beginns[i] in current_id:
website_guess_str = '' + website_eq[i] + ''
self.website_guess.setOpenExternalLinks(True)
break
self.website_guess.setText(website_guess_str)
self.save_button.clicked.connect(self.on_save_button_clicked)
self.later_button.clicked.connect(self.on_later_button_clicked)
self.never_button.clicked.connect(self.on_never_button_clicked)
try:
os.remove('temp/currentObject/partner_url.txt')
except:
pass
#pyqtSlot()
def on_never_button_clicked(self):
addNeverID(getFromFile("id.txt"))
saveToFile("Never-ID", "partner_url.txt")
self.close()
def on_later_button_clicked(self):
saveToFile("Later-ID", "partner_url.txt")
self.close()
def on_save_button_clicked(self):
url_is_valid = False
for i in getWebsites():
if i in self.partner_url_input.text():
url_is_valid = True
break
if url_is_valid:
saveToFile(self.partner_url_input.text(), "partner_url.txt")
self.close()
else:
error_dialog = QtWidgets.QErrorMessage(self)
error_dialog.setWindowTitle("Eingabe nicht verwertbar")
error_dialog.showMessage('Die eingegebene URL ist nicht verwendbar! Bitte prüfe deine Eingabe.')
def showGUI():
app = QApplication(sys.argv)
app.setStyle('Fusion')
app.setWindowIcon(QtGui.QIcon('lib/icon.png'))
window = Start()
app.exec_()
def saveToFile(content, filename):
file = open("temp/currentObject/" + filename, "w+")
file.write(content)
file.close()
def getFromFile(filename):
file = open("temp/currentObject/" + filename)
content = file.read()
file.close()
return content
Many thanks in advance
The reason is that since you're using uic, it automatically enables the auto-connection feature, which automatically detects function names based on object/signals names and connects them, even if the functions do not have Qt slots decorators.
The result is that your slot will be actually called thrice:
without any argument (clicked());
with the checked argument (clicked(bool)): the argument is ignored by Qt since the function doesn't take any, but the function will be called anyway because no slot signature has been specified for it;
again with the checked argument, because you manually connected it in your code;
If you want to keep using the auto connection, use a unique slot decorator for that specific function, otherwise manually connect to a function (possibly with a slot, if you need a specific signature) that does not use the auto connection naming, but don't use both.
class askForPartnerUrl(QDialog):
def __init__(self):
super(askForPartnerUrl, self).__init__()
loadUi('askForPartnerURL.ui', self)
# ...
# remove the following lines:
# self.save_button.clicked.connect(self.on_save_button_clicked)
# self.later_button.clicked.connect(self.on_later_button_clicked)
# self.never_button.clicked.connect(self.on_never_button_clicked)
# manual connection
self.later_button.clicked.connect(self.saveLater)
# using the auto connection; the function doesn't need arguments, so
# you can ignore the argument type signature
#pyqtSlot()
def on_never_button_clicked(self):
addNeverID(getFromFile("id.txt"))
# ...
# with a normal function; in this case no slot decorator is required since
# you don't have arguments
def saveLater(self):
url_is_valid = False
# ...
PS: The reason for which it gets "stuck" is probably due to the way Python deals with the end of the program (which by default happens as soon as the last window is closed in Qt) on MacOS: after the first call to close() PyQt tries to quit the QApplication (free up memory, etc...), but while doing so the original click event is still in the process of firing the signals to the remaining second and third slot, hence the "loop" (but it's not an actual loop, and the third slot never gets called because it's the second one that blocks everything).
Note that this is a big oversimplification, I'm not an expert in memory usage and low level programming, but this is fundamentally what's happening.
I have a simple application that I'm writing using PyQt 4 that includes a MainWindow that consists of a QMdiArea. When the user starts a new session (new experiment), two windows are generated. The user can also select from a list of analysis modules to also run.
I do not want any duplicates of any windows to be allowed (i.e., in the example code below the user can only have one file_tree_window, one data_plot_window, and/or one cell_monitor_window).
So, for example, if the user selects "New Experiment" once, two windows are generated. If he/she then selects "New Experiment" again, those original two windows should be closed and two new ones opened. The same is true for the any of the "Analysis Modules." (i.e. selecting one that is already open will cause the open one to close and a new one to open).
I'm having trouble generating this functionality though.
First off, code:
class MainWindow(QtGui.QMainWindow):
def __init__(self, model):
super().__init__()
self.resize(1400, 800)
self.model = model
menubar = self.menuBar()
new_experiment_action = QtGui.QAction("New Experiment", self)
new_experiment_action.triggered.connect(self.setup_new_experiment)
file_menu = menubar.addMenu("File")
file_menu.addAction(new_experiment_action)
cell_health_action = QtGui.QAction("Cell Health Monitor", self)
cell_health_action.triggered.connect(self.setup_cell_health_window)
analysis_menu = menubar.addMenu("Analysis Modules")
analysis_menu.addAction(cell_health_action)
self.file_tree = None
self.file_tree_window = None
self.data_plot = None
self.data_plot_window = None
self.cell_health = None
self.cell_health_window = None
self.mdi = QtGui.QMdiArea()
self.setCentralWidget(self.mdi)
def setup_new_experiment(self):
self.mdi.closeAllSubWindows()
self.file_tree = FileTree(self.model)
self.file_tree.setMinimumSize(QtCore.QSize(200, 300))
self.data_plot = DataPlot(self.model)
self.data_plot.setMinimumSize(self.size()*.4)
self.file_tree_window = self.mdi.addSubWindow(self.file_tree)
self.file_tree_window.show()
self.data_plot_window = self.mdi.addSubWindow(self.data_plot)
self.data_plot_window.show()
def setup_cell_health_window(self):
if self.cell_health_window:
self.mdi.removeSubWindow(self.cell_health_window)
self.cell_health = CellHealthMonitor(self.model)
self.cell_health.setMinimumSize(self.size()*.3)
self.cell_health_window = self.mdi.addSubWindow(self.cell_health)
self.cell_health_window.show()
This doesn't work in a number of ways:
If I open a "Cell Health Monitor" window, close it, and try to
re-open it, Python crashes
If I have a "Cell Health Monitor" window open, and then try to start a "New Experiment", followed by opening a "Cell Health Monitor" window, Python crashes
I have tried to use remove() instead of close():
i.e.:
def setup_new_experiment(self):
for window in self.mdi.subWindowList():
self.mdi.removeSubWindow(window)
self.file_tree = FileTree(self.model)
self.file_tree.setMinimumSize(QtCore.QSize(200, 300))
self.data_plot = DataPlot(self.model)
self.data_plot.setMinimumSize(self.size()*.4)
self.file_tree_window = self.mdi.addSubWindow(self.file_tree)
self.file_tree_window.show()
self.data_plot_window = self.mdi.addSubWindow(self.data_plot)
self.data_plot_window.show()
def setup_cell_health_window(self):
if self.cell_health_window:
self.mdi.removeSubWindow(self.cell_health_window)
self.cell_health = CellHealthMonitor(self.model)
self.cell_health.setMinimumSize(self.size()*.3)
self.cell_health_window = self.mdi.addSubWindow(self.cell_health)
self.cell_health_window.show()
Depending on the series of actions Python either crashes or I get one of the following two error messages:
(this one is actually just a warning):
QMdiArea::removeSubWindow: window is not inside workspace
or
Traceback (most recent call last):
File "MainWindow.py", line 61, in setup_cell_health_window
self.mdi.removeSubWindow(self.cell_health_window)
RuntimeError: wrapped C/C++ object of type QMdiSubWindow has been deleted
I'm at a loss with regards to what to do differently to remove a subwindow without producing these issues.
Thanks
If I set the current folder via the method Gtk.FileChooserWidget.set_current_folder(), the first time I open the file chooser, it opens on the location used as argument for set_current_folder()
But, if I select a file, the I re-open the file-chooser, it opens on the "most_recent_used_files".
I'd like it opens on the last selected file's folder path.
How to do it?
Thank you.
From the docs:
Old versions of the file chooser's documentation suggested using gtk_file_chooser_set_current_folder() in various situations, with the intention of letting the application suggest a reasonable default folder. This is no longer considered to be a good policy, as now the file chooser is able to make good suggestions on its own. In general, you should only cause the file chooser to show a specific folder when it is appropriate to use gtk_file_chooser_set_filename() - i.e. when you are doing a File/Save As command and you already have a file saved somewhere.
You may or may not like the reasoning for this behavior. If you're curious about how it came about, see File chooser recent-files in the mailing list and Help the user choose a place to put a new file on the GNOME wiki.
Setting the current folder each time works for me, but it is a little tricky. I'm using Gtk 3.14 and Python 2.7.
You have to get the filename before resetting the directory, or it's lost, and the current directory may be None, so you have to check for that.
This code is tested on Debian jessie and Windows 7.
import os.path as osp
from gi.repository import Gtk
class FileDialog(Gtk.FileChooserDialog):
def __init__(self, parent, title):
Gtk.FileChooserDialog.__init__(self, title, parent)
self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
self.add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
self.set_current_folder(osp.abspath('.'))
def __call__(self):
resp = self.run()
self.hide()
fname = self.get_filename()
d = self.get_current_folder()
if d:
self.set_current_folder(d)
if resp == Gtk.ResponseType.OK:
return fname
else:
return None
class TheApp(Gtk.Window):
def on_clicked(self, w, dlg):
fname = dlg()
print fname if fname else 'canceled'
def __init__(self):
Gtk.Window.__init__(self)
self.connect('delete_event', Gtk.main_quit)
self.set_resizable(False)
dlg = FileDialog(self, 'Your File Dialog, Sir.')
btn = Gtk.Button.new_with_label('click here')
btn.connect('clicked', self.on_clicked, dlg)
self.add(btn)
btn.show()
if __name__ == '__main__':
app = TheApp()
app.show()
Gtk.main()
I have a GUI designed in glade, using python/gtk in the background.I want to handle the delete event and display a "Are you sure?"-message dialog.I have been trying to handle the delete and destroy events, but failing to do so.any light?
#!/usr/bin/python
import .... stuff
class App:
def __init__(self):
self.gladefile = 'test.glade'
windowname = 'window'# This must match the window name in glade
self.wTree = gtk.glade.XML(self.gladefile, windowname)# object for acessing widgets
dic={
# Also need to set project2's signal tab
'on_window_delete_event':self.on_erro,
'on_window_destroy_event':self.on_erro,
}
self.wTree.signal_autoconnect (dic)
self.op=self.wTree.get_widget('window')
self.op.show()
def on_erro(self,widget,*args):
print 'hello'
app = App()
gtk.main()
This code opens a simple window .On clicking on close button, it prints hello and exits.(I want the window to remain open)
You have to return True in order to stop propagation of the delete event in the callback on_erro as mentioned in the documentation for "delete-event". In your current code, the callback is not returning any boolean value as required by the function, which I am guessing is returning False (Please check the signature for on_window_delete_event callback functions, the return type is boolean)
Hope this helps!
I have extended the UI file resulting from the Plugin builder with Qt Creator.
Just added some checkboxes and a combobox, named layercombo to the form.
The application is named jacktest.py. It uses an intermediate file jackdialog.py (generated from the plugin builder, left unchanged).
Compiled the UI file and the resource file. Then added some code to the plugin and tested this. It's no problem to get the available layer names in a QMessagebox. But how to add these to the combobox ?
Should be simple, but no option succeeds in referencing the combobox.
Error message: AttributeError: jacktest instance has no attribute 'layercombo'.
Result from my latest try:
# run method that performs all the real work
def run(self):
# create and show the dialog
dlg = jacktestDialog()
# show the dialog
dlg.show()
result = dlg.exec_()
for layer in self.iface.legendInterface().layers():
if layer.type() == QgsMapLayer.VectorLayer:
QMessageBox.information( self.iface.mainWindow(), "Info", layer.name())
self.layercombo.Items.Insert(0, layer.name())
# See if OK was pressed
if result == 1:
# do something useful (delete the line containing pass and
# substitute with your code
pass
You are trying to reference the current class (which is not your dialog) when you are setting the layercombo items
Replace:
self.layercombo.Items.Insert(0, layer.name())
with
dlg.ui.layercombo.Items.Insert(0, layer.name())
but you code still won't work correctly as exec_() is blocking and waits until it returns so you are adding items to an invisible dialog.
Try this instead:
# create and show the dialog
dlg = jacktestDialog()
# show the dialog
for layer in self.iface.legendInterface().layers():
if layer.type() == QgsMapLayer.VectorLayer:
QMessageBox.information( self.iface.mainWindow(), "Info", layer.name())
dlg.ui.layercombo.Items.Insert(0, layer.name())
result = dlg.exec_()
Went on in developing a Signal within the run module (code: def run (self):)
QObject.connect(dlg.ui.layercombo,SIGNAL('currentIndexChanged (int)'),self.select_one)
and the code of select_one is:
def select_one(self):
comboindex = dlg.ui.layercombo.currentIndex()
QMessageBox.information(self.iface.mainWindow(), "Info", comboindex)
Error message:
comboindex = dlg.ui.layercombo.currentIndex()
NameError: global name 'dlg' is not defined
Suppose I have to reference dlg as a parameter in the function call, but this is not working until now.