I am trying to convert PyQT4 code to PyQt5 which requires new style of connecting signals. When I use:
self.connect(okButton, SIGNAL("clicked()"),form, SLOT("accept()"))
self.connect(cancelButton, SIGNAL("clicked()"),form, SLOT("reject()"))
everything works as expected - by clicking OK or Cancel, dialog window will close. With a new style sygnals (where i guess I am doing something wrong)
okButton.clicked.connect(self.accept)
cancelButton.clicked.connect(self.reject)
after clicking Ok/Cancel, main window will close and dialog will stay. Which is definitely not what should happen.
What am I doing wrong?
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout
# from PyQt4.QtCore import *
# from PyQt4.QtGui import *
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
myButton = QPushButton("open window")
self.myFirstLabel = QLabel("First Window")
layout = QVBoxLayout()
layout.addWidget(myButton)
layout.addWidget(self.myFirstLabel)
self.setLayout(layout)
myButton.clicked.connect(self.openNewWindow)
self.setWindowTitle("SecondWindowTest")
def openNewWindow(self):
myLabel = QLabel("Second Window")
okButton = QPushButton("&OK")
cancelButton = QPushButton("Cancel")
buttonLayout = QHBoxLayout()
buttonLayout.addStretch()
buttonLayout.addWidget(okButton)
buttonLayout.addWidget(cancelButton)
layout = QGridLayout()
layout.addWidget(myLabel, 0, 0)
layout.addLayout(buttonLayout, 1, 1, 1, 2)
form = QDialog()
form.setLayout(layout)
okButton.clicked.connect(self.accept)
cancelButton.clicked.connect(self.reject)
# self.connect(okButton, SIGNAL("clicked()"),form, SLOT("accept()"))
# self.connect(cancelButton, SIGNAL("clicked()"),form, SLOT("reject()"))
form.setWindowTitle("Second Window")
if form.exec_():
self.myFirstLabel.setText('OK was pressed')
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
The equivalent of the following connection:
self.connect(okButton, SIGNAL("clicked()"),form, SLOT("accept()"))
self.connect(cancelButton, SIGNAL("clicked()"),form, SLOT("reject()"))
In the new style of connection is:
okButton.clicked.connect(form.accept)
cancelButton.clicked.connect(form.reject)