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

(Pyqt) create instances of widgets generated in designer



  • Hi,
    I' still new to pyqt so I dont know if I am making some basic mistake, but I cannot find a solution to my problem anywhere. I created a widget using the qframe template in designer and I converted it to a python file like the one below. My problem is that I dont know how to create an instance of this widget. My Main window has a grid layout and I want to add this widget to it. Im only not doing things manually because I will have multiple instances of this widget. Can some one tell me a simple way to insert this widget to my GUI? also I dont know why the code is setting a size, I'd immagine the main window would dictate the widgets size

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'GraphTemplate.ui'
    #
    # Created by: PyQt5 UI code generator 5.15.0
    #
    # WARNING: Any manual changes made to this file will be lost when pyuic5 is
    # run again.  Do not edit this file unless you know what you are doing.
    
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_Frame(object):
        def setupUi(self, Frame):
            Frame.setObjectName("Frame")
            Frame.resize(640, 480)
            Frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
            Frame.setFrameShadow(QtWidgets.QFrame.Raised)
            self.gridLayout = QtWidgets.QGridLayout(Frame)
            self.gridLayout.setObjectName("gridLayout")
            self.widget = PlotWidget(Frame)
            self.widget.setObjectName("widget")
            self.gridLayout.addWidget(self.widget, 0, 0, 1, 1)
    
            self.retranslateUi(Frame)
            QtCore.QMetaObject.connectSlotsByName(Frame)
    
        def retranslateUi(self, Frame):
            _translate = QtCore.QCoreApplication.translate
            Frame.setWindowTitle(_translate("Frame", "Frame"))
    from pyqtgraph import PlotWidget
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Frame = QtWidgets.QFrame()
        ui = Ui_Frame()
        ui.setupUi(Frame)
        Frame.show()
        sys.exit(app.exec_())
    
    


  • I managed to create an instance, but I am still unable to insert the widget into the main gui. this is the code I am using along with the code above. I haven't been able to find a way to add the widget, i get this error, that I dont know how to fix.

        self.UI.MainTabGraphGrid.addWidget(self.Graph1, 0, 0, 1, 1)
    TypeError: arguments did not match any overloaded call:
      addWidget(self, QWidget): argument 1 has unexpected type 'Ui_Form'
      addWidget(self, QWidget, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'Ui_Form'
      addWidget(self, QWidget, int, int, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'Ui_Form'
    
    class AppGUI():
        def __init__(self):
            self.App = QApplication(sys.argv) # config setup?
            self.MainWindow = QMainWindow()
            self.UI = MasterGUI.Ui_MainWindow()
            self.UI.setupUi(self.MainWindow)
    
            self.Graph1 = GraphTemplate.Ui_Form()
            self.Graph1.setupUi(QtWidgets.QFrame())
            self.UI.MainTabGraphGrid.addWidget(self.Graph1, 0, 0, 1, 1)
    
            self.MainWindow.show()
            sys.exit(self.App.exec_())
    


  • @kiyoshi7 said in (Pyqt) create instances of widgets generated in designer:

    i get this error, that I dont know how to fix.

    As the error message states, your self.gridLayout.addWidget(self.widget, 0, 0, 1, 1) call does not match any available overload of QGridLayout.addWidget(). And it even shows you which ones are available, what the types of the arguments are, and which argument is causing it a problem!

    The Ui_... classes generated from Designer are not widgets. Have a look in the generated ui_...py files to see, then it's not such a mystery. They are wrappers which set up a widget passed as an argument. So soemthing like:

    self.graph1 = QtWidgets.QFrame()
    ui = GraphTemplate.Ui_Form()
    ui.setupUi(self.graph1)
    self...addWidget(self.graph1, 0, 0, 1, 1)
    

    However, this corresponds to the first example code in https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html. You would be better adopting the pattern in the second or third examples there. Where, for each .ui form you design you edit your .py file (not ui...py) to create a dedicated class for the form and use that as the widget.

    I haven't got time to go through and show exact code. You'll need to understand and follow the example, or find some online.


Log in to reply