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. Embed form .ui file made with Qt Designer into a QFrame of a dialog window made with QtDesigner
Forum Updated to NodeBB v4.3 + New Features

Embed form .ui file made with Qt Designer into a QFrame of a dialog window made with QtDesigner

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.7k Views 2 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.
  • E Offline
    E Offline
    Ernest
    wrote on last edited by Ernest
    #1

    Hello. I'm currently working on a Python project where I will have a main dialog window with a frame in it. In this frame, I could insert one of many different forms (which I'm designing in Qt Designer) depending on the situation. Is there a way to create separate .ui files for each of these forms and embed them into the frame of the main dialog window?
    Thanks

    JonBJ 1 Reply Last reply
    0
    • E Ernest

      Hello. I'm currently working on a Python project where I will have a main dialog window with a frame in it. In this frame, I could insert one of many different forms (which I'm designing in Qt Designer) depending on the situation. Is there a way to create separate .ui files for each of these forms and embed them into the frame of the main dialog window?
      Thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Ernest
      Easiest way: If you are using your pyuic to generate a Python class from the .ui file you have a QWidget-derived class. You create an instance dynamically and add it onto your dialog window's frame or wherever.

      1 Reply Last reply
      1
      • E Offline
        E Offline
        Ernest
        wrote on last edited by
        #3

        Great. How do I dynamically add it into my dialog window's frame?

        JonBJ 1 Reply Last reply
        0
        • E Ernest

          Great. How do I dynamically add it into my dialog window's frame?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Ernest
          Well I don't know exactly what your ".dialog window's frame" is. You can add what you have designed wherever you can add any QWidget. Your generated code for some foo.ui file will give you a

          class Foo(QWidget):
          

          so e.g. you can go:

          foo = Foo()
          dialog.layout.addWidget(foo)
          

          as appropriate wherever you want it. The point being, if you could do it with a widget from code, you can do it from a widget designed in Designer, which is what you asked about.

          1 Reply Last reply
          1
          • E Offline
            E Offline
            Ernest
            wrote on last edited by
            #5

            Thanks! To be more specific, my main window is a QDialog, the frame I'm trying to add a form to is a QFrame inside of said dialog, and the form I'm trying to embed that was created with Qt Designer is also a QFrame.
            Unfortunately, I couldn't get your code to work. "layout" appears to be a method, and thus has no "addWidget" attribute. I then tried calling the method instead with

            dialog.layout().addWidget(foo)
            

            But layout() appears to return None since I get "AttributeError: 'NoneType' object has no attribute 'addWidget'"

            I tried doing the same, to the frame object itself, ie

            dialog.frame.layout().addWidget(foo)
            dialog.frame.layout.addWidget(foo)
            

            Same results.

            JonBJ 1 Reply Last reply
            0
            • E Offline
              E Offline
              Ernest
              wrote on last edited by
              #6

              I made some progress. I fixed the issue where layout() was returning "None" by doing

              dialog.frame.setLayout(QHBoxLayout())
              

              Then

              dialog.frame.layout().addWidget(foo)
              

              Now it doesn't throw an error but the form I made also doesn't appear in the frame at all. It's still empty.

              1 Reply Last reply
              0
              • E Ernest

                Thanks! To be more specific, my main window is a QDialog, the frame I'm trying to add a form to is a QFrame inside of said dialog, and the form I'm trying to embed that was created with Qt Designer is also a QFrame.
                Unfortunately, I couldn't get your code to work. "layout" appears to be a method, and thus has no "addWidget" attribute. I then tried calling the method instead with

                dialog.layout().addWidget(foo)
                

                But layout() appears to return None since I get "AttributeError: 'NoneType' object has no attribute 'addWidget'"

                I tried doing the same, to the frame object itself, ie

                dialog.frame.layout().addWidget(foo)
                dialog.frame.layout.addWidget(foo)
                

                Same results.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Ernest said in Embed form .ui file made with Qt Designer into a QFrame of a dialog window made with QtDesigner:

                Unfortunately, I couldn't get your code to work.

                It was showing you what you needed to do, not code to copy, hence "as appropriate". Since we don't even know what you've put in your dialog we couldn't give you exact code if we wanted to.

                E 1 Reply Last reply
                0
                • JonBJ JonB

                  @Ernest said in Embed form .ui file made with Qt Designer into a QFrame of a dialog window made with QtDesigner:

                  Unfortunately, I couldn't get your code to work.

                  It was showing you what you needed to do, not code to copy, hence "as appropriate". Since we don't even know what you've put in your dialog we couldn't give you exact code if we wanted to.

                  E Offline
                  E Offline
                  Ernest
                  wrote on last edited by
                  #8

                  @JonB Like I said, it's just a

                  class SomeDialog(QtWidgets.QDialog)
                  

                  containing a QFrame element.
                  The form I'm trying to embed into this dialog is a

                  class SomeFrame(QtWidgets.QFrame)
                  

                  with some buttons in it. I had already instantiated this frame with

                  foo = SomeFrame()
                  

                  I managed to get

                  dialog.frame.layout().addWidget(foo)
                  

                  to work but the dialog's frame still appears empty. Is there any other information you need?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    StephvanS
                    wrote on last edited by
                    #9

                    Has anyone had success with this?
                    I have a tabwidget on a MainWindow.
                    Inside the tabs I want to instantiate different forms I created and converted to python.
                    I can get the individual forms to render IF I don't use the tabwidget.

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

                      @StephvanS Hi and welcome to devnet,

                      The likely problem that was discussed here is that frame was neither shown nor added to a layout applied to dialog.

                      As for your issue, you should open your own thread showing your code.

                      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
                      1

                      • Login

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