Embed form .ui file made with Qt Designer into a QFrame of a dialog window made with QtDesigner
-
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 -
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 -
@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 anyQWidget
. Your generated code for somefoo.ui
file will give you aclass 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.
-
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 withdialog.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.
-
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.
-
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 withdialog.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.
@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.
-
@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.
@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 aclass 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?
-
@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.