Using a custom PySide2 widget in Qt Designer

后端 未结 1 607
失恋的感觉
失恋的感觉 2021-01-05 23:22

I\'m searching for a way to use custom widgets in Qt Designer written with Qt for Python (PySide2) effectively.

I found out, that it is possible to design the GUI wi

1条回答
  •  被撕碎了的回忆
    2021-01-05 23:31

    There are 2 main ways to use a custom widget in Qt Designer:

    1. Promoting the widget:

    This is the simplest and least laborious way, if it's an internal widget you just have to right click on the widget and select Promote To ..., then in:

    • Base Class Name choose the class from which you inherit
    • Promoted Class Name place the name of the class a
    • Header File place the path of the file changing the extension .py to .h

    What the above does is generate the following:

    ...
        
    ...
     
      
       RadialBar
       QWidget
       
    radialbar.h
    1
    ...

    That is, change class, and add a field in customwidget pointing to the name of the class: NAME_OF_CLASS , name of the class they inherit CLASS_EXTENDS and file path changing the file extension from .py to .h

    PATH_OF_FILE.h
    , that is, the same data as You entered the form.

    In the case of the root widget it can not be done through Qt Designer, but I see that you have understood the logic but you have not modified everything correctly, your main mistake is to point out the class from which they inherit

     
      
       MyMainWindow
       QMainWindow <----
       
    mymainwindow.h
    1

    So the correct file is:

    custom_widget_modified.ui

    
    
     MainWindow
     
      
       
        0
        0
        800
        600
       
      
      
       MainWindow
      
      
       
        
         0
         0
         155
         15
        
       
       
        
         
          
           TextLabel
          
         
        
        
         
          
           TextLabel
          
         
        
        
         
          
           TextLabel
          
         
        
       
      
      
       
        
         0
         0
         800
         21
        
       
       
        
         File
        
        
       
       
      
      
       
        
         0
         0
         3
         18
        
       
      
      
       
        Exit
       
      
     
     
      
       MyMainWindow
       QMainWindow
       
    mymainwindow.h
    1
    actionExit triggered() MainWindow close() -1 -1 399 299

    mymainwindow.py

    import sys
    
    from PySide2.QtUiTools import QUiLoader
    from PySide2.QtWidgets import QApplication, QMainWindow, QMessageBox
    from PySide2.QtCore import QFile
    
    
    class MyMainWindow(QMainWindow):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.setWindowTitle("Demo QtWidget App")
    
        def closeEvent(self, event):
            msgBox = QMessageBox()
            msgBox.setWindowTitle("Quit?")
            msgBox.setText("Exit application?")
            msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
            msgBox.setDefaultButton(QMessageBox.No)
            ret = msgBox.exec_()
            if ret == QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()
    
    
    if __name__ == '__main__':
        app = QApplication([])
        file = QFile("custom_widget_modified.ui")
        file.open(QFile.ReadOnly)
        loader = QUiLoader()
        loader.registerCustomWidget(MyMainWindow)
        main_window = loader.load(file)
        main_window.show()
        sys.exit(app.exec_())
    

    2. Promoting the widget:

    0 讨论(0)
提交回复
热议问题