Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Best Practice to load .ui files

Best Practice to load .ui files

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 1.5k Views
  • 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.
  • alomA Offline
    alomA Offline
    alom
    wrote on last edited by alom
    #1

    I've seen a lot of mixed methods on the web for importing ui into a custom class inhered from a QMainWindow or widget. In this example let us use a QMainWindow where I'd be build a large application. I'm trying to find the best practices for using a ui file.

    I'm aware of the conversion of the ui to .py files with pyqt5, but I really like the flexibility to pop into designer and update the ui file and see instance changes.
    So far I've been using this method:

    from PySide2 import QtCore, QtWidgets, QtUiTools
    import os
    
    class ImportUi(QtWidgets.QMainWindow):
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            loader = QtUiTools.QUiLoader()
            script_dir = os.path.dirname(__file__)
            main_ui_path = 'gui.ui'
            self.ui = loader.load(os.path.join(script_dir, main_ui_path))
    
            print(self.ui.named_table_widget)
    
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        gui = ImportUi()
        gui.ui.show()
        rec = app.exec_()
        sys.exit(rec)
    

    Is there a way the ImportUi class can inherit from the returned QMainWindow from the loader? So I would not have to store the ui in the class instance variable? Basically is there a way i could access self.named_table_widget without having to go through the self.ui? I've seen you can pass a parent widget to the .load() argument, but using "self" didn't give me the results i was hoping for.
    Or is there a completely better approach to all of this?

    Cheers

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      No there is not. QUiLoader::load returns a new QWidget that you can then use. The parent parameter just makes said widget a child of the parent.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • alomA Offline
        alomA Offline
        alom
        wrote on last edited by
        #3

        Thanks SGaist, just wanted to make sure I wasn't missing something.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • alomA alom

          Thanks SGaist, just wanted to make sure I wasn't missing something.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @alom said in Best Practice to load .ui files:

          Thanks SGaist, just wanted to make sure I wasn't missing something

          If your issue is solved, please don't forget to mark your post as such!.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1

          • Login

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