Close Event are not called Keyboard Event and MessageBox - python

I programming with QT Designer a GUI and would like disable the Close Event with ESC-Key. The User has the possibility to close the Application with X on the top but not with any Keys.
The Function closeEvent(self,event) are not working.
def closeEvent(self, event):
close = QMessageBox()
close.setText("You sure?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
event.accept()
else:
event.ignore()
The function are on my main class: class Ui_Tool(object)
class Ui_LabelTool(object):
def setupUi(self, LabelTool):
Tool.setObjectName("Tool")
Tool.resize(650, 569)
Tool.setMinimumSize(QtCore.QSize(650, 569))
Tool.setMaximumSize(QtCore.QSize(650, 569))
Tool.setAutoFillBackground(False)
Tool.setSizeGripEnabled(False)
...
#Events for Buttons
self.SelectFolder.clicked.connect(self.setfolder)
self.SelectOutputFolder.clicked.connect(self.SetOutputFolder)
self.LoadeMeasurement.clicked.connect(self.LoadRecording)
self.StartButton.clicked.connect(self.startProcessing)
self.Next_btn.clicked.connect(self.nextOperation)
self.Prev_Btn.clicked.connect(self.prefOperation)
self.Reset_btn.clicked.connect(self.resetApp)
self.treeView.clicked.connect(self.ClickMeasurement)
self.treeWidget.clicked.connect(self.CheckTopicSelect)
self.horizontalSlider.valueChanged.connect(self.SliderValueChange)
self.horizontalSlider.sliderMoved.connect(self.dispSlider)
self.BBObject_btn.clicked.connect(self.CreateBBObj)
self.BBNoObject_btn.clicked.connect(self.CreateBBNoObj)
self.ShowAll_btn.clicked.connect(self.SaveImages)
def retranslateUi(self, LabelTool):
_translate = QtCore.QCoreApplication.translate
Tool.setWindowTitle(_translate("Tool", "Tool"))
self.LoadeMeasurement.setText(_translate("Tool", "Load Measurement"))
self.StartButton.setText(_translate("Tool", "Start "))
self.Reset_btn.setText(_translate("Tool", "Reset"))
self.Header_lbl.setText(_translate("Tool", "Test"))
...
And this is my main Function:
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Tool = QtWidgets.QDialog()
ui = Ui_Tool()
ui.setupUi(Tool)
Tool.show()
sys.exit(app.exec_())
What I do wrong?
I add the following class:
class Logic(QMainWindow, Ui_Tool):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
def closeEvent(self, event):
answer = QtWidgets.QMessageBox.question(
self,
'Are you sure you want to quit ?',
'Task is in progress !',
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()

If the docs is reviewed:
Escape Key
If the user presses the Esc key in a dialog, QDialog::reject() will be
called. This will cause the window to close: The close event cannot be
ignored.
So there are 2 possible solutions:
Override keyPressEvent() method, when you press the escape key, call close().
Override reject() method to make the verification of the QMessageBox and according to it make its logic.
In addition to this implementation you must do it in the widget and not in the class generated by Qt Designer(1), considering the above the implementations of both solutions are:
1.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LabelTool(object):
def setupUi(self, Tool):
Tool.setObjectName("Tool")
Tool.resize(650, 569)
Tool.setMinimumSize(QtCore.QSize(650, 569))
Tool.setMaximumSize(QtCore.QSize(650, 569))
Tool.setAutoFillBackground(False)
Tool.setSizeGripEnabled(False)
# ....
class LabelTool(QtWidgets.QDialog, Ui_LabelTool):
def __init__(self, parent=None):
super(LabelTool, self).__init__(parent)
self.setupUi(self)
def verify_by_user(self):
answer = QtWidgets.QMessageBox.question(
self,
"Are you sure you want to quit ?",
"Task is in progress !",
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No,
)
return answer == QtWidgets.QMessageBox.Yes
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.close()
else:
super(LabelTool, self).keyPressEvent(event)
def closeEvent(self, event):
if self.verify_by_user():
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = LabelTool()
w.show()
sys.exit(app.exec_())
2.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LabelTool(object):
def setupUi(self, Tool):
Tool.setObjectName("Tool")
Tool.resize(650, 569)
Tool.setMinimumSize(QtCore.QSize(650, 569))
Tool.setMaximumSize(QtCore.QSize(650, 569))
Tool.setAutoFillBackground(False)
Tool.setSizeGripEnabled(False)
# ....
class LabelTool(QtWidgets.QDialog, Ui_LabelTool):
def __init__(self, parent=None):
super(LabelTool, self).__init__(parent)
self.setupUi(self)
def verify_by_user(self):
answer = QtWidgets.QMessageBox.question(
self,
"Are you sure you want to quit ?",
"Task is in progress !",
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No,
)
return answer == QtWidgets.QMessageBox.Yes
def reject(self):
if self.verify_by_user():
super(LabelTool, self).reject()
def closeEvent(self, event):
if self.verify_by_user():
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = LabelTool()
w.show()
sys.exit(app.exec_())
(1) Using the Generated Code

Related

How to allow tab key pressing event in pyqt5

Assuming that I have a QPushButton named button, I successfully do the following to allow click event:
class UI(object):
def setupUi(self, Dialog):
# ...
self.button.clicked.connect(self.do_something)
def do_something(self):
# Something here
My question is: how can I call do_something() when the tab key is pressed? For instance, if I have a QlineEdit named tb_id, after entering a value and press tab key, do_something() method should be called in the same way as what clicked does above. How can I do that using pyqt5?
Thank you so much.
To get what you want there are many methods but before pointing it by observing your code I see that you have generated it with Qt Designer so that code should not be modified but create another class that uses that code so I will place the base code:
from PyQt5 import QtCore, QtWidgets
class UI(object):
def setupUi(self, Dialog):
self.button = QtWidgets.QPushButton("Press Me")
lay = QtWidgets.QVBoxLayout(Dialog)
lay.addWidget(self.button)
class Dialog(QtWidgets.QDialog, UI):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.button.clicked.connect(self.do_something)
#QtCore.pyqtSlot()
def do_something(self):
print("do_something")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
Also, I recommend you read the difference between event and signal in the world of Qt in What are the differences between event and signal in Qt since you speak of click event but in the world of Qt one must say clicked signal.
Now if going to the point there are the following options:
Using keyPressEvent:
class Dialog(QtWidgets.QDialog, UI):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.button.clicked.connect(self.do_something)
#QtCore.pyqtSlot()
def do_something(self):
print("do_something")
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Tab:
self.do_something()
Using an event filter:
class Dialog(QtWidgets.QDialog, UI):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.button.clicked.connect(self.do_something)
#QtCore.pyqtSlot()
def do_something(self):
print("do_something")
def eventFilter(self, obj, event):
if obj is self and event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Tab:
self.do_something()
return super(Dialog, self).eventFilter(obj, event)
Using activated signal of QShorcut:
class Dialog(QtWidgets.QDialog, UI):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setupUi(self)
self.button.clicked.connect(self.do_something)
shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Tab), self)
shortcut.activated.connect(self.do_something)
#QtCore.pyqtSlot()
def do_something(self):
print("do_something")
From the previous methods I prefer the latter because you do not need to overwrite anything and you can connect to several functions.
On the other hand, only events are detected when the focus is in the Qt window.
I assume you put your widget in QDialog widget, so if you want to implement your own key press event, you should override the keyPressEvent of your Dialog widget,
then it can behave as you like.
Here's an example,
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog
class UI(QDialog):
def __init__(self):
super(UI, self).__init__()
# ...
self.button.clicked.connect(self.do_something)
def do_something(self):
# Something here
def keyPressEvent(self, event):
# when press key is Tab call your function
if event.key() == Qt.Key_Tab:
self.do_something()

Cannot close Dialog on Python PyQt4

My program has a MainWindow which contains my inventory system. I have added a Dialog window which pop-ups when I clicked "Add Item". I am able to successfully open the Dialog window but I can't seem to close it.
When the user tries to close the Dialog Window, it will display a messagebox asking if the user really wants to close the window. Currently, I'm using self.close(). It just closes the MessageBox I've made to prevent accidental exit and doesn't close the Dialog window unless you end it using the IDE or task manager.
Here's my code snippets:
Main.py
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.db = Database()
self.model = Model(self)
self.ui = MainWindow_ui()
self.ui.setupUi(self)
self.window = Ui_Dialog()
self.ui.addItem.clicked.connect(lambda : self.start_Form())
def start_Form(self):
window = QtGui.QDialog()
self.window.setupUi(window)
self.window.show()
def main():
app = QtGui.QApplication(sys.argv)
window = Main()
window.showMaximized()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
AddItem.py (contains the Dialog Window code)
def getNumber():
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('SELECT seq FROM sqlite_sequence')
itemNumber = c.fetchone()[0]
return int(itemNumber) + 1
class Ui_Dialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(413, 382)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setStandardButtons(
QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Reset)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
def retranslateUi(self, Dialog):
self.itemCode.setText(str(getNumber()))
def accept(self):
row = self.mapper.currentIndex()
self.mapper.submit()
self.main.model.insertRow(row)
self.mapper.setCurrentIndex(row)
self.close()
def reject(self):
ret = QtGui.QMessageBox.question(None, 'Close request', 'Are you sure you want to quit?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
if ret == QtGui.QMessageBox.Yes:
self.close()
else:
pass
Your accept() and reject() methods effectively create an infinite loop, because calling close() will, in turn, just call those methods again. When overriding virtual methods, you should call the base-class implementation using super:
class Ui_Dialog(QtGui.QDialog):
...
def accept(self):
...
super(Ui_Dialog, self).accept()
def reject(self):
...
if ret == QtGui.QMessageBox.Yes:
super(Ui_Dialog, self).reject()

How to contol KeyEvent in Qt

With the window declared with CustomWidget as super class: class App(CustomWidget) hitting Alt+A properly prints 'keyPressEvent: Alt + a' message.
But the KeyEvent functionality is broken when the CustomWidget is assigned to window with setCentralWidget() or is set with layer.addWidget(widget). What is missing in a code?
from PyQt4 import QtCore, QtGui
class CustomWidget(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent=parent)
def keyPressEvent(self, event):
if event.modifiers() == QtCore.Qt.AltModifier:
if event.key() == QtCore.Qt.Key_A:
print 'keyPressEvent: Alt + a'
# super(CustomWidget, self).keyPressEvent(event)
class App(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent=parent)
centralWidget = CustomWidget(self)
self.setCentralWidget(centralWidget)
mainLayout=QtGui.QVBoxLayout()
centralWidget.setLayout(mainLayout)
widget = CustomWidget(self)
mainLayout.addWidget(widget)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = App()
w.show()
sys.exit(app.exec_())
The widget must have focus to receive the event. Make sure you call setFocusPolicy() to have the CustomWidget accept and maintain focus after creating the window.
QWidget, keyPressEvent
QWidget, setFocusPolicy
Working Solution:
Important: At the end of GroupBox' keyPressEvent() method we have to pass the Event up to the super. Or the Event will not get propagated to the parent widget: super(QtGui.QGroupBox, self).keyPressEvent(event)
import sys
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent):
QtGui.QMainWindow.__init__(self, parent=parent)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
def keyPressEvent(self, event):
if event.modifiers() == QtCore.Qt.ControlModifier:
if event.key() == QtCore.Qt.Key_T:
print 'MainWindow: Control + t'
if event.key() == QtCore.Qt.Key_M:
print 'MainWindow: Control + m'
class GroupBox(QtGui.QGroupBox):
def __init__(self, parent=None):
QtGui.QGroupBox.__init__(self, parent=parent)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
def keyPressEvent(self, event):
if event.modifiers() == QtCore.Qt.ControlModifier:
if event.key() == QtCore.Qt.Key_T:
print 'GroupBox: Control + t'
if event.key() == QtCore.Qt.Key_S:
print 'GroupBox: Control + s'
super(QtGui.QGroupBox, self).keyPressEvent(event)
class App(MainWindow):
def __init__(self, parent=None):
MainWindow.__init__(self, parent=parent)
centralWidget = QtGui.QWidget(self)
self.setCentralWidget(centralWidget)
mainLayout=QtGui.QVBoxLayout()
centralWidget.setLayout(mainLayout)
groupBox = GroupBox(self)
mainLayout.addWidget(groupBox)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = App()
w.show()
sys.exit(app.exec_())

PyQt4 - Dragging and dropping files into QPushButton

I think the title is fairly self explanatory. I'm working to create a small standalone app that requires the user to drag and drop audio files onto buttons to in turn associate the file with a corresponding button on a piece of hardware by using the filepath, etc...
I've followed a ton of drag and drop tutorials for widgets, and my friend has for lists, however I'm beginning to believe that it can't be done for a button? I'm aware that you can drag and drop text onto a button. I am not fully up to speed with Qt yet so there may just be a glaring error that I'm missing.
Here is the code, many thanks!
import sys
from PyQt4 import QtGui, QtCore
class Button(QtGui.QPushButton):
def __init__(self, parent):
super(Button, self).__init__(parent)
self.setAcceptDrops(True)
self.setDragDropMode(QAbstractItemView.InternalMove)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
super(Button, self).dragEnterEvent(event)
def dragMoveEvent(self, event):
super(Button, self).dragMoveEvent(event)
def dropEvent(self, event):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
path = self.addItem(url.path())
print path
event.acceptProposedAction()
else:
super(Button,self).dropEvent(event)
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,300,400)
self.setWindowTitle("Filenames")
self.btn = QtGui.QPushButton()
self.btn.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.btn.setText("Change Me!")
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
There are three problems with your posted code, the main being that you aren't even using the custom Button class that you made. You are adding just a regular button to your window with:
self.btn = QtGui.QPushButton()
instead of:
self.btn = Button(self)
Also, QPushButtons don't have a setDragDropMode() method, so you'll need to comment that line out. I'm not sure what it does anyway.
Also, QPushButton doesn't have an addItem() method so I'm not sure what that's about unless you were planning on implementing it. I replaced it below with just printing the file path.
Here is a working version of your code, that just prints the file path of any file dragged into the button:
import sys
from PyQt4 import QtGui, QtCore
class Button(QtGui.QPushButton):
def __init__(self, parent):
super(Button, self).__init__(parent)
self.setAcceptDrops(True)
#self.setDragDropMode(QAbstractItemView.InternalMove)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
super(Button, self).dragEnterEvent(event)
def dragMoveEvent(self, event):
super(Button, self).dragMoveEvent(event)
def dropEvent(self, event):
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
print str(url.toLocalFile())
event.acceptProposedAction()
else:
super(Button,self).dropEvent(event)
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,300,400)
self.setWindowTitle("Filenames")
self.btn = Button(self)
self.btn.setGeometry(QtCore.QRect(90, 90, 61, 51))
self.btn.setText("Change Me!")
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.btn)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())

How to change minimize event behavior in PyQt or PySide?

I'm developing a Qt application and changed the closing behavior with the closeEvent virtual function this way:
class MainWindow(QMainWindow):
def closeEvent(self, event):
event.ignore()
self.hide()
self.trayicon.showMessage('Running', 'Running in the background.')
This works as expected. If I remove event.ignore() the application quits as expected, everything is fine.
I want to control the minimize event too, so when the user clicks the minimize button on the title bar, I want to move the window instead of minimize.
I cannot use the hideEvent virtual function, because the event will be sent to the window anyway, so this code:
def hideEvent(self, event):
event.ignore()
self.move(0,0)
moves the window to the top left AND then minimize it. event.ignore() has no effect here, so I tried using QtCore.QObject.event this way:
def event(self, event):
if event.type() == QEvent.WindowStateChange:
if self.isMinimized():
event.ignore()
self.move(0,0)
return True
return False
The window moves but minimizes again. What is wrong with this ? How can I override the minimize event completely ?
Try the changeEvent and filter for WindowMinimized events, something like this:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.systemTrayIcon = QtGui.QSystemTrayIcon(self)
self.systemTrayIcon.setIcon(QtGui.QIcon.fromTheme("face-smile"))
self.systemTrayIcon.setVisible(True)
self.systemTrayIcon.activated.connect(self.on_systemTrayIcon_activated)
self.label = QtGui.QLabel(self)
self.label.setText("Minimize me!")
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.label)
#QtCore.pyqtSlot(QtGui.QSystemTrayIcon.ActivationReason)
def on_systemTrayIcon_activated(self, reason):
if reason == QtGui.QSystemTrayIcon.DoubleClick:
if self.isHidden():
self.show()
else:
self.hide()
def changeEvent(self, event):
if event.type() == QtCore.QEvent.WindowStateChange:
if self.windowState() & QtCore.Qt.WindowMinimized:
event.ignore()
self.close()
return
super(MyWindow, self).changeEvent(event)
def closeEvent(self, event):
event.ignore()
self.hide()
self.systemTrayIcon.showMessage('Running', 'Running in the background.')
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.show()
sys.exit(app.exec_())

Categories