2

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!!"))
  • They do not work since [QMainWindow](https://doc.qt.io/qt-5/qmainwindow.html) has no `accept` or `reject` functions as [QDialog](https://doc.qt.io/qt-5/qdialog.html) has. While they are both considered top level windows, they are meant for different purposes. So, the real question is: why do you want to use a QMainWindow instead of a QDialog? That said, pyuic generated files should never, ***EVER*** be modified, as it's considered bad practice for a lot of reasons. Please follow the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Nov 09 '21 at 03:46
  • Understood the pyuic file should never be touched (was just testing commands). I choose QMainWindow over QDialog as I was wanting to add a tool menu, and I'm using Designer to build the page. I can see the tool menu is easy to setup in QMainWinow, but I can't see any widget to add one in QDialog within Designer (so manual build to add it). I can see a good manual workaround for this on https://stackoverflow.com/questions/18435801/can-you-add-a-toolbar-to-qdialog/28343489#28343489 But this is a manual option without using Designer. – user17362664 Nov 09 '21 at 11:16
  • You either use a QDialog and add toolbars manually, or you use QMainWindow and create your own functions that would do what `accept` and `reject` would do, which is setting a result value and call `close()`. – musicamante Nov 09 '21 at 11:42
  • After a good review of my GUI design, and why I'm using the OK / Cancel buttons, I decided it was a stupid idea to use them on the main page, but great and easy to use in QDialog if called. BUT great clarification on the reasons to use QMainWindow over QDialog as your primary window. I can see if you want to have a menu / toolbar on your primary page (which I do), then this is the easier way to go over using QDialog since you can use Qt Designer to set it all up and generate the code. Thank you mujsicamante for the great clarification! – user17362664 Nov 11 '21 at 02:53
  • consider that nothing prevents you to have [some] standard features of a QMainWindow on a basic QWidget (or QDialog): if you use a layout manager (and you should), you can use [`setMenuBar()`](https://doc.qt.io/qt-5/qlayout.html#setMenuBar) on *any* QLayout subclass, and you can also add QToolBars and QStatusBar to any layout as well. You still need to do that programmatically and add proper support for them, but it's still something you can do. As always, it depends on your requirements, and experience (and studying the documentation) can only help you with that. – musicamante Nov 11 '21 at 03:22

0 Answers0