Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How use .ui files in PyQT?
Forum Updated to NodeBB v4.3 + New Features

How use .ui files in PyQT?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    How use .ui files in PyQT?
    I find this, but here no file ui_mainwindow and code not work.

    class MainWindow(QMainWindow):
        def __init__(self):
            # super(MainWindow, self).__init__()
            # self.ui = Ui_MainWindow()
            # self.ui.setupUi(self)
            ui_file = QFile("./mainwindow.ui")
            ui_file.open(QFile.ReadOnly)
    
            loader = QUiLoader()
            window = loader.load(ui_file)
            window.show()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
    
        window = MainWindow()
        # window.show()
    
        sys.exit(app.exec_())
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mikeeeeee
      wrote on last edited by
      #2

      sory, its work:

      import sys
      from PySide2.QtUiTools import QUiLoader
      from PySide2.QtWidgets import QApplication
      from PySide2.QtCore import QFile, QIODevice
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
      
          ui_file_name = "mainwindow.ui"
          ui_file = QFile(ui_file_name)
          if not ui_file.open(QIODevice.ReadOnly):
              print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString()))
              sys.exit(-1)
          loader = QUiLoader()
          window = loader.load(ui_file)
          ui_file.close()
          if not window:
              print(loader.errorString())
              sys.exit(-1)
          window.show()
      
          sys.exit(app.exec_())
      
      JonBJ 1 Reply Last reply
      0
      • M Mikeeeeee

        sory, its work:

        import sys
        from PySide2.QtUiTools import QUiLoader
        from PySide2.QtWidgets import QApplication
        from PySide2.QtCore import QFile, QIODevice
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
        
            ui_file_name = "mainwindow.ui"
            ui_file = QFile(ui_file_name)
            if not ui_file.open(QIODevice.ReadOnly):
                print("Cannot open {}: {}".format(ui_file_name, ui_file.errorString()))
                sys.exit(-1)
            loader = QUiLoader()
            window = loader.load(ui_file)
            ui_file.close()
            if not window:
                print(loader.errorString())
                sys.exit(-1)
            window.show()
        
            sys.exit(app.exec_())
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Mikeeeeee
        You are dynamically loading your ui file at run-time. That is fine, but more code for you to write, no code completion at design-time, no class/member variables etc. for the form, you must supply the ui file at run-time, and so on.

        Are you aware that you can --- if you wish --- use the pyuic5 program supplied with PyQt5 to produce Python/PyQt5 code for your ui files instead of loading them dynamically at runtime, with corresponding advantages? An explanation is at https://stackoverflow.com/questions/52471705/why-in-pyqt5-should-i-use-pyuic5-and-not-uic-loaduimy-ui, with clickable link for pyuic5 to get you going.

        1 Reply Last reply
        1
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          use pyuic5 -x interface.ui and then inspect the __main__ created to test the interface. That should enlighten you as to how to instantiate it, if you understand python.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

            Are there any working examples with pyuic5?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by Mikeeeeee
              #6

              It is not work

              class CdViewer(QObject):
                  def __init__(self):
                      super(CdViewer, self).__init__()
                      uic.loadUi("cdViewer.ui", self)
                      self.show()
              

              It is work, but how I can work with widgets and get signals?

              if __name__ == '__main__':
                  app = Qt.QApplication([])
                  win = uic.loadUi("cdViewer.ui")
                  win.show()
                  app.exec()
              
              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                It is works:

                
                from PyQt5 import QtCore, QtGui, QtWidgets, uic
                
                from PyQt5 import Qt
                # from PyQt5 import QtNetwork
                from PySide2.QtCore import QObject
                # from PyQt5.QtCore import QCoreApplication, QUrl
                import sys
                
                class CdViewer(QObject):
                    # ui = uic.loadUi("cdViewer.ui")
                
                
                    def __init__(self):
                        # super(CdViewer, self).__init__()
                        self.ui = uic.loadUi("cdViewer.ui")
                        self.ui.label_13.setText("asdf")
                
                    def showWindow(self):
                        self.ui.show()
                
                
                
                if __name__ == '__main__':
                    app = Qt.QApplication([])
                    # win = uic.loadUi("cdViewer.ui")
                    # win.show()
                    # sys.exit(app.exec())
                    cdViewer = CdViewer()
                    cdViewer.showWindow()
                    # cdViewer.show()
                    app.exec()
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved