I create the UI in Qt Designer.
If I use the "Dialog with Bottom Buttons" template, this works perfectly (with all the right modifications). If i use "Main Window" template, it opens and all looks good, it creates the window and buttons, but clicking the OK or Cancel button does nothing.
I tried adding the following to simpleMain.py, but this just results in errors, but I don't think I should have to edit this file manually:
self.buttonBox.accepted.connect(MainWindow.accept) # type: ignore #manual add
self.buttonBox.rejected.connect(MainWindow.reject) # type: ignore #manual add
AttributeError: 'QMainWindow' object has no attribute 'accept'
I'm guessing this is due to QDialog not being available, but if I add this I get an error:
"QDialog is not accessed", so not much point in adding.
Files:
simpleMain.ui
pyuic5 -o simpleMain.py simpleMain.ui
=>simpleMain.py
Main: simpleMain-A.py
simpleMain-A.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from simpleMain import Ui_MainWindow # Import class Ui_MainWindow from simpleMain.py
# simpleMain.py is located in the same directory as main
class AppWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.show()
sys.exit(app.exec_())
- simpleMain.ui
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(404, 365)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(70, 80, 221, 81))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.buttonBox = QtWidgets.QDialogButtonBox(self.centralwidget)
self.buttonBox.setGeometry(QtCore.QRect(230, 320, 156, 23))
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
#self.buttonBox.accepted.connect(MainWindow.accept) # type: ignore #manual add
#self.buttonBox.rejected.connect(MainWindow.reject) # type: ignore #manual add
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "It Opened the Window!!"))