Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt designer window not showing



  • HI. Have been playing around with PyQt5 tutorials. Discovered Qt Designer. Designed a basic layout, again following a tutorial. Cannot get it to show. Any pointers? What am I missing?

    Thanks

    Program:

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import QDialog, QApplication, QWidget

    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.combox = QtWidgets.QComboBox(self.centralwidget)
    self.combox.setGeometry(QtCore.QRect(40, 120, 111, 31))
    font = QtGui.QFont()
    font.setPointSize(10)
    self.combox.setFont(font)
    self.combox.setObjectName("combox")
    self.combox.addItem("")
    self.combox.addItem("")
    self.comboy = QtWidgets.QComboBox(self.centralwidget)
    self.comboy.setGeometry(QtCore.QRect(230, 120, 111, 31))
    font = QtGui.QFont()
    font.setPointSize(10)
    self.comboy.setFont(font)
    self.comboy.setObjectName("comboy")
    self.comboy.addItem("")
    self.comboy.addItem("")
    self.Submit = QtWidgets.QPushButton(self.centralwidget)
    self.Submit.setGeometry(QtCore.QRect(140, 410, 111, 31))
    font = QtGui.QFont()
    font.setPointSize(10)
    self.Submit.setFont(font)
    self.Submit.setObjectName("Submit")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(130, 250, 141, 61))
    font = QtGui.QFont()
    font.setPointSize(12)
    self.label.setFont(font)
    self.label.setObjectName("label")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        self.InitUI_MainWindow()
    
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.combox.setItemText(0, _translate("MainWindow", "0"))
        self.combox.setItemText(1, _translate("MainWindow", "1"))
        self.comboy.setItemText(0, _translate("MainWindow", "0"))
        self.comboy.setItemText(1, _translate("MainWindow", "1"))
        self.Submit.setText(_translate("MainWindow", "Submit"))
        self.label.setText(_translate("MainWindow", "x xor y ="))
    

    if name == "main":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Ui_MainWindow = QtWidgets.QMainWindow
    ui = Ui_MainWindow()
    sys.exit(app.exec_())


  • Banned

    It is a sad thing that you have discovered the Designer as it is not a python-pyqt friendly application -- it was created to create XML pages and then converts it into extremely ugly horrible to use python-pyqt code that does not follow python-pyqt coding methodologies -- if you are wanting to learn (for free) how to create python-pyqt programs and have access to Discord shoot me a friend request DenniO#8137 and I will get you solid ongoing help that will help you learn to fish ;) -- will not give you a fish but will help you learn how to properly use a fishing pole, which bait is best, and where to go to fish -- catching the fish will be totally up to you


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From a quick look you don't call the show method of your UI_MainWindow object anywhere, therefore nothing is shown.



  • Hi.

    Denni O. I have picked up vaguely that Designer was going to be a pig to work with. I just found the learning process of PyQt fairly hard work, though maybe I just need to find the right tutorial.

    SGaist. Thank you. Ok will have a look at this.


  • Banned

    @SandraB if you want help with Python-Pyqt you can shoot me a message on Discord at DenniO#8173 and I can help you (for free) directly or in a more classroom like setting -- as I am quite aware of the issues about learning the basics of PyQt in a manner that is conducive to its use


  • Banned

    @SandraB I was tooling along and thought I got time so first I figured out how to get your Designer version working and then I created a much cleaner more straight forward version of your design using proper Python-PyQt however in my version since you were not using the MenuBar or StatusBar I just dropped them -- further you layout did not have your Submit doing anything so I added that as well -- hope this helps you sort out how to use PyQt for real rather than that garbage the Designer creates

    If you have absolutely any questions do ask I would more than happy to help you get a more solid grasp of PyQt

    Your Code Fixed To Work

    from PyQt5.QtCore    import *
    from PyQt5.QtGui     import *
    from PyQt5.QtWidgets import *
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.combox = QComboBox(self.centralwidget)
            self.combox.setGeometry(QRect(40, 120, 111, 31))
            font = QFont()
            font.setPointSize(10)
            self.combox.setFont(font)
            self.combox.setObjectName("combox")
            self.combox.addItem("")
            self.combox.addItem("")
            self.comboy = QComboBox(self.centralwidget)
            self.comboy.setGeometry(QRect(230, 120, 111, 31))
            font = QFont()
            font.setPointSize(10)
            self.comboy.setFont(font)
            self.comboy.setObjectName("comboy")
            self.comboy.addItem("")
            self.comboy.addItem("")
            self.Submit = QPushButton(self.centralwidget)
            self.Submit.setGeometry(QRect(140, 410, 111, 31))
            font = QFont()
            font.setPointSize(10)
            self.Submit.setFont(font)
            self.Submit.setObjectName("Submit")
            self.label = QLabel(self.centralwidget)
            self.label.setGeometry(QRect(130, 250, 141, 61))
            font = QFont()
            font.setPointSize(12)
            self.label.setFont(font)
            self.label.setObjectName("label")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QMenuBar(MainWindow)
            self.menubar.setGeometry(QRect(0, 0, 800, 21))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
            QMetaObject.connectSlotsByName(MainWindow)
    
    #        self.InitUI_MainWindow()
    
        def retranslateUi(self, MainWindow):
            _translate = QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.combox.setItemText(0, _translate("MainWindow", "0"))
            self.combox.setItemText(1, _translate("MainWindow", "1"))
            self.comboy.setItemText(0, _translate("MainWindow", "0"))
            self.comboy.setItemText(1, _translate("MainWindow", "1"))
            self.Submit.setText(_translate("MainWindow", "Submit"))
            self.label.setText(_translate("MainWindow", "x xor y ="))
            
    if __name__ == '__main__':
        app = QApplication([])
    
        MainWindow = QMainWindow()
        UI_Window = Ui_MainWindow()
        UI_Window.setupUi(MainWindow)
        MainWindow.show()
    
        app.exec()
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    

    .
    My Version using Proper PyQt

    from PyQt5.QtGui     import QFont
    from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout, QHBoxLayout
    from PyQt5.QtWidgets import QStyleFactory, QComboBox, QPushButton
    
    class CenterPanel(QWidget):
        def __init__(self, parent):
            QWidget.__init__(self)
            self.Parent = parent
            # -----
            Font10 = QFont()
            Font10.setPointSize(10)
            # -----
            Font12 = QFont()
            Font12.setPointSize(12)
            # -----
            self.cbxXVal = QComboBox()
            self.cbxXVal.setFont(Font10)
            self.cbxXVal.addItem('0')
            self.cbxXVal.addItem('1')
            self.cbxXVal.setEditable(False)
            self.cbxXVal.setCurrentIndex(0)
            # -----
            self.cbxYVal = QComboBox()
            self.cbxYVal.setFont(Font10)
            self.cbxYVal.addItem('0')
            self.cbxYVal.addItem('1')
            self.cbxYVal.setEditable(False)
            self.cbxYVal.setCurrentIndex(0)
            # -----
            self.lblXOR = QLabel('  XOR  ')
            self.lblXOR.setFont(Font12)
            # -----
            hbxXORLay = QHBoxLayout()
            hbxXORLay.addStretch(1)
            hbxXORLay.addWidget(self.cbxXVal)
            hbxXORLay.addWidget(self.lblXOR)
            hbxXORLay.addWidget(self.cbxYVal)
            hbxXORLay.addStretch(1)
            # -----
            self.Submit = QPushButton('Submit')
            self.Submit.clicked.connect(self.Submitted)
            self.Submit.setFont(Font10)
            hbxSubmit = QHBoxLayout()
            hbxSubmit.addStretch(1)
            hbxSubmit.addWidget(self.Submit)
            hbxSubmit.addStretch(1)
            # -----
            self.lblOutput = QLabel('x xor y = ')
            self.lblOutput.setFont(Font12)
            hbxOutput = QHBoxLayout()
            hbxOutput.addStretch(1)
            hbxOutput.addWidget(self.lblOutput)
            hbxOutput.addStretch(1)
            # -----
            vbxLayout = QVBoxLayout()
            vbxLayout.addStretch(1)
            vbxLayout.addLayout(hbxXORLay)
            vbxLayout.addLayout(hbxSubmit)
            vbxLayout.addLayout(hbxOutput)
            vbxLayout.addStretch(1)
            # -----
            self.setLayout(vbxLayout)
    
        def Submitted(self):
            XVal = self.cbxXVal.currentText()
            YVal = self.cbxYVal.currentText()
            Rslt = int(XVal) ^ int(YVal)
            TextOut = XVal + ' xor ' + YVal + ' = ' + str(Rslt)
    
            self.lblOutput.setText(TextOut)
    
    class MainWindow(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.setWindowTitle('Main Window')
            self.resize(200, 200)
            
            self.CenterPane = CenterPanel(self)
            self.setCentralWidget(self.CenterPane)
    
    if __name__ == '__main__':
        MainThred = QApplication([])
        MainThred.setStyle(QStyleFactory.create("Cleanlooks"))
        
        MainApp = MainWindow()
        MainApp.show()
    
        MainThred.exec()
    
      # If anyone wants more extensive free help I run an online lab-like classroom-like 
      # message server feel free and drop by you will not be able to post until I clear 
      # you as a student as this prevents spammers so if interested here is the invite
      # https://discord.gg/3D8huKC
    

    Perhaps one of the biggest differences you will notice is that your layout does not respond at all to a windows resizing which is not how windows these days work ;)


Log in to reply