[SOLVED] Mixing hand coded GUI with Qt Designer GUI
-
Answers to the original question have been placed in-line to make it clearer to others.
I'm using Qt 5.3.2 (Qt Creator 3.2.1) on Windows. I've got a form created from Qt Designer and a MyLabel class (that extends QLabel) that I want to appear in a given location on the form.What is the best way to:
(a) maintain the GUI layout look (so it reflects how the GUI will look when it is run), and
(b) add my own MyLabel widget to a given location on the form.I currently have a "place holder" widget (QTextEdit) because it will automatically expand to the size the layout manager asks, and I can set the min/max size to appropriately reflect the GUI layout I'm trying to achieve and then at run-time I swap/replace the QTextEdit widget with my widget as shown below:
@
MyLabel* myLabel = new MyLabel( this );
ui->topLevelHorizLayout->replaceWidget( ui->txtReplaceMe, myLabel );
@(Q) Is there another easier way to do this? (A) Yes, using "Promoted widgets"::http://qt-project.org/doc/qt-5/designer-using-custom-widgets.html#promoting-widgets.
(Q) Does anyone have any pointers to Wiki, Forum, or documentation that highlights how best to mix subclassed widgets with GUI forms? (A) See "Promoted widgets"::http://qt-project.org/doc/qt-5/designer-using-custom-widgets.html#promoting-widgets.
I've looked at several postings and search results, and it seems like the "supported way" to do this is to have a "custom widget", see "Designer-Creating-Custom-Widgets":http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html.
(Q) Do I need to create custom-widgets in order to get the result I'm looking for? (A) No, by using "Promoted widgets"::http://qt-project.org/doc/qt-5/designer-using-custom-widgets.html#promoting-widgets you can get the desired result.
I've also looked at:
"Good post about trade-offs of hand-coded vs GUI generated solutions":http://stackoverflow.com/questions/387092/hand-coded-gui-versus-qt-designer-gui"QTabWidget not displayed":http://qt-project.org/forums/viewthread/4464
-
All you need to do is to use the "promote widget" function from Qt Creator (Designer): "link":http://qt-project.org/doc/qt-5/designer-using-custom-widgets.html#promoting-widgets. The name of this function is perhaps not very intuitive, but it should do exactly what you need.
-
Wow, this is perfect. Thanks for the quick reply!
This is all documented in "the link sierdzio gave me":http://qt-project.org/doc/qt-5/designer-using-custom-widgets.html#promoting-widgets. I just didn't realize it until Sierdzio pointed it out to me. Thanks Sierdzio!
Solution: Layout your GUI the way you want, and for your custom objects pick an underlying Widget that is close to what you want (so the designer GUI looks similar to what it will look like at run-time).
Next right click on the widget that you want to be "your widget" and select "Promote to ...". Conceptually, you can add as many of "YourWidget" classes as you want to this dialog box.
When you are done click on your widget's name and click the "Promote" button. This tells the Qt Designer instead of creating a Qt widget, call your code (class) to create the widget. It gets better because all of the other design time properties you set on the widget are also set on your widget. I found this out by looking at the generated code in ui_MyLabel.h in the setupUi() method.
-
Hey, I'm glad your problem is solved. Happy coding! :-)