I have a Qt Application with multiple main windows and want to get a signal when the App is about to quit while all windows are still up.
The QApplication.aboutToQuit signal doesn't work for me because it only fires after all windows have been closed.
All other answers I've seen suggest implementing the main window's closeEvent() function, but I have multiple main windows and I couldn't find any way to differentiate CloseEvents between a normal closeEvent of just a single window being closed and a close event from when the whole application is closing after QMD+Q or the using clicking quit in the task bar or for whatever reason it quits.
How do I get a signal for that only when the whole application is about to quit but before any windows are closed?
What happens when I press Cmd+Q or right click quit the task bar icon:
(I want a signal at this point) <-
all windows get a closeEvent()
aboutToQuit() fires
app quits
What I want is to get a signal before any of that happens and all the windows are still open.
Edit: minimal example
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtWidgets import QApplication, QWidget
class Win(QWidget):
def closeEvent(self, event: QCloseEvent):
# implementing a custom closeEvent doesn't help me
# this is called for every normal manual window close and when the application quits
# I only want to run something when the full application gets shut down, not just this window
# can't see any way to differentiate between the two
print("closeEvent", event.type(), event)
return super().closeEvent(event)
if __name__ == '__main__':
app = QApplication([])
app.aboutToQuit.connect(lambda :print("about to quit, this is too late, by now all windows are closed"))
#What I need
# app.beforeQuitSignal.connect(lambda :print("signal that it's gonna quit, before any of the windows are closed"))
#stuff I've tried that didn't work either
app.quit = lambda *args:print("quit, doesnt get called")
app.exit = lambda *args:print("exit, doesnt get called")
app.closeAllWindows = lambda *args:print("closeAllWindows, doesnt get called")
mainwins = []
for i in range(5):
win = Win()
win.setGeometry(100*i,100*i, 400,400), win.show(), win.raise_()
mainwins.append(win)
app.exec_()
If you want to fire a signal when the user close a window but do not exit the QApplication immediately after the last window is closed, you can use QApplication.setQuitOnLastWindowClosed()
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtWidgets import QApplication, QWidget
class Win(QWidget):
closing = pyqtSignal()
def closeEvent(self, event: QCloseEvent):
print("Window {} closed".format(self))
self.closing.emit()
return super().closeEvent(event)
class MyApp(QApplication):
def __init__(self, *args):
super(MyApp, self).__init__(*args)
self.setQuitOnLastWindowClosed(False)
self.lastWindowClosed.connect(self.onLastClosed)
self.mainwins = []
for i in range(5):
win = Win()
win.setGeometry(100 * i, 100 * i, 400, 400), win.show(), win.raise_()
self.mainwins.append(win)
def onLastClosed(self):
print("Last windows closed, exiting ...")
self.exit()
if __name__ == '__main__':
app = MyApp([])
app.exec_()
With this, when a window is closed, a closing() signal is emited
You can filter on the CloseEvent if it is spontaneous :
from PyQt5.QtCore import pyqtSignal, QEvent
from PyQt5.QtGui import QCloseEvent
from PyQt5.QtWidgets import QApplication, QWidget
class Win(QWidget):
closing = pyqtSignal()
def closeEvent(self, event: QCloseEvent):
self.closing.emit()
if event.type() == QEvent.Close and event.spontaneous():
event.ignore()
else:
return super().closeEvent(event)
class MyApp(QApplication):
def __init__(self, *args):
super(MyApp, self).__init__(*args)
self.mainwins = []
for i in range(5):
win = Win()
win.closing.connect(self.beforeQuit)
win.setGeometry(100 * i, 100 * i, 400, 400), win.show(), win.raise_()
self.mainwins.append(win)
def beforeQuit(self):
print("Exiting")
# Do what you want here ...
self.exit() # Terminate the QApplication
if __name__ == '__main__':
app = MyApp([])
app.exec_()
Use installEventFilter to intercept the application's quit event before any windows are closed:
class AppManager(QObject):
def __init__(self):
super().__init__()
qApp.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.Quit:
self.saveWindowStates()
return False
def saveWindowStates(self):
# code to save the application's state
Related
I found this sample code using QThread at https://doc.qt.io/qtforpython/examples/example_widgets__thread_signals.html
I modified the code in an attempt to pass a variable to the thread when instantiated based on info I found here: https://nikolak.com/pyqt-threading-tutorial/
Unfortunately I am getting the following error:
Traceback (most recent call last):
File "C:\Users\joe\python\ThreadTest.py", line 22, in start_thread
instanced_thread = WorkerThread(self, 'c:\testfile.xml')
File "C:\Users\joe\python\ThreadTest.py", line 44, in __init__
QThread.__init__(self, parent)
TypeError: 'PySide6.QtCore.QThread.__init__' called with wrong argument types:
PySide6.QtCore.QThread.__init__(str)
Supported signatures:
PySide6.QtCore.QThread.__init__(Optional[PySide6.QtCore.QObject] = None)
Can someone tell me the proper syntax for passing a variable to a QThread?
I'm using PySide6 and Python 3.9.10.
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import sys
from PySide6.QtCore import QObject, QThread, Signal, Slot
from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
# Create a basic window with a layout and a button
class MainForm(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My Form")
self.layout = QVBoxLayout()
self.button = QPushButton("Click me!")
self.button.clicked.connect(self.start_thread)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
# Instantiate and start a new thread
def start_thread(self):
instanced_thread = WorkerThread(self, 'c:\testfile.xml')
instanced_thread.start()
# Create the Slots that will receive signals
#Slot(str)
def update_str_field(self, message):
print(message)
#Slot(int)
def update_int_field(self, value):
print(value)
# Signals must inherit QObject
class MySignals(QObject):
signal_str = Signal(str)
signal_int = Signal(int)
# Create the Worker Thread
class WorkerThread(QThread):
def __init__(self, filename, parent=None):
QThread.__init__(self, parent)
# Instantiate signals and connect signals to the slots
self.signals = MySignals()
self.signals.signal_str.connect(parent.update_str_field)
self.signals.signal_int.connect(parent.update_int_field)
self.filename = filename
def run(self):
# Do something on the worker thread
a = 1 + 1
# Emit signals whenever you want
self.signals.signal_int.emit(a)
self.signals.signal_str.emit("This text comes to Main thread from our Worker thread.")
print(self.filename)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainForm()
window.show()
sys.exit(app.exec())
On Windows, there are several key bindings for standard actions. For example, to copy, one can use Ctrl+C or Ctrl+Insert.
How to handle that with Qt ? This is what I did:
I got the list of key bindings with QKeySequence.keyBindings().
I created 2 actions, one for Ctrl+C, another for Ctrl+Insert.
It seems to work.
Question: Is it the right way to handle key bindings with Qt ?
Full source code:
from sys import argv, exit
from PyQt4.QtGui import QApplication, QWidget, QAction, QKeySequence
class Widget(QWidget):
def __init__(self):
QWidget.__init__(self)
for key in QKeySequence.keyBindings(QKeySequence.Copy):
action = QAction("Copy", self)
action.triggered.connect(self._copy)
action.setShortcut(key)
self.addAction(action)
def _copy(self):
print("Copy!")
print("On Windows, use Ctrl+C or Ctrl+Insert to copy.")
app = QApplication(argv)
w = Widget()
w.show()
exit(app.exec_())
You only need one action and a call to QAction::setShortcuts().
action = QAction("Copy", self)
action.setShortcuts(QKeySequence.keyBindings(QKeySequence.Copy))
action.triggered.connect(self._copy)
I started a project in Qt Creator initially with a C++ backend, but then switched it to use PyQt5. I have a main.qml, where when I press a button called Exit, I call Qt.quit().
However, I get a General Message stating: Signal QQmlEngine::quit() emitted, but no receivers connected to handle it.
My question is, how do I receive this signal and handle it?
Code:
main.py:
import sys
import PyQt5
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtQml
from PyQt5.QtCore import QObject pyqtSignal
class DestinyManager,(QtGui.QGuiApplication):
"""the app self"""
def __init__(self, argv):
super(DestinyManager, self).__init__(argv)
# Define a new signal called 'trigger' that has no arguments.
trigger = pyqtSignal()
def connect_and_emit_trigger(self):
# Connect the trigger signal to a slot.
self.trigger.connect(self.handle_trigger)
self.menuItem_Exit.clicked.connect(self.close)
# Emit the signal.
self.trigger.emit()
def handle_trigger(self):
# Show that the slot has been called.
print("trigger signal received")
def main(argv):
app = DestinyManager(sys.argv)
engine = QtQml.QQmlEngine(app)
component = QtQml.QQmlComponent(engine)
component.loadUrl(QtCore.QUrl("exit.qml"))
topLevel = component.create()
if topLevel is not None:
topLevel.show()
else:
for err in component.errors():
print(err.toString())
app.exec()
if __name__ == '__main__':
QObject,main(sys.argv)
Exit.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
Window {
Button {
id: btn_Exit
text: "Exit"
onClicked: Qt.quit();
}
}
There are a few syntax errors in the python script, but ignoring those, the code can be made to work like this:
def main(argv):
app = DestinyManager(sys.argv)
engine = QtQml.QQmlEngine(app)
engine.quit.connect(app.quit)
...
Which is to say, you simply need to connect the qml quit signal to an appropriate slot in your python script.
I have connected a QPushButton to a method that call file dialog. The simplified code look like this:
def init_buttons(self):
self.browse_button = QPushButton('&Browse')
self.browse_button.clicked.connect(self.browse_file)
def browse_file(self):
file_name = QFileDialog.getExistingDirectory()
# Just for checking
print(file_name)
Sometimes QFileDialog won't showing up. The process is indeed running, since the main class/widget doesn't response to my clicking. Sometimes it's showing up.
If QFileDialog doesn't show up, with pycharm, I have to stop and kill process to end the program. If I run the program directly from terminal, I have to manually end the running process to end the program. I can't figure out what causing this, since terminal not showing any exception or warning.
So, what is this?
The parameters for the getExistingDirectory were wrong. Please try this. Also, I have added further information in my pull request.
import os
def browse_file(self):
self.save_dir = QFileDialog.getExistingDirectory(self,
"Open Save Directory", os.path.expanduser('~'))
print(self.save_dir)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
QAction,QMessageBox, QFileDialog, QApplication,QPushButton,QInputDialog,QLineEdit)
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.fileName=""
self.text=""
btn1 = QPushButton("Encrypt", self)
btn1.clicked.connect(self.onBtn1)
self.show()
def onBtn1(self):
self.fileName, _ = QFileDialog.getOpenFileName(self, 'Open file', '/Users/Jarvis/Desktop/')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
I have a PyQt widget that sends signals with numpy.ndarray data. And I have another PyQt widget that has a slot with numpy.ndarray data.
Both widget are located on my main window, that is compiled from *.ui file. The widgets are set as promoted widgets.
Cannot I somehow connect the signal and slot in Qt Creator?
Just now it gives me the next error:
TypeError: C++ type 'ndarray' is not supported as a slot argument type
Reason for this is Qt only support the datatype defined in QMetaType passed as argument of signal, looks here http://pyqt.sourceforge.net/Docs/PyQt4/qmetatype.html#Q_DECLARE_METATYPE
According to ekhumoro's POST, I update the following code, it should work for PyQt4, not tested on PyQt5.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from numpy import *
class MyThread(QThread):
mysignal = pyqtSignal(ndarray)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
def run(self):
while True:
QThread.msleep(100)
self.mysignal.emit(array((1, 2, 3, 4)))
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
self.thread = MyThread()
self.thread.mysignal.connect(self.handleSignal)
self.thread.start()
def handleSignal(self, data):
print data
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = MyWidget()
w.show()
app.exec_()