Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved How use .ui files in PyQT?

    General and Desktop
    3
    7
    410
    Loading More Posts
    • 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
      Mikeeeeee last edited by

      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 Reply Quote 0
      • M
        Mikeeeeee last edited by

        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_())
        
        JonB 1 Reply Last reply Reply Quote 0
        • JonB
          JonB @Mikeeeeee last edited by JonB

          @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 Reply Quote 1
          • Kent-Dorfman
            Kent-Dorfman last edited by

            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 Reply Quote 0
            • M
              Mikeeeeee last edited by

              Are there any working examples with pyuic5?

              1 Reply Last reply Reply Quote 0
              • M
                Mikeeeeee last edited by Mikeeeeee

                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 Reply Quote 0
                • M
                  Mikeeeeee last edited by

                  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 Reply Quote 0
                  • First post
                    Last post