How to load a QtDesigner ui form into a QFrame of another ui form? - qt

I have designed a QWidget based QtDesigner ui form. Lets call it Form1. I added few pushbuttons and labels in it. Now I designed another QWidget based qt ui designer form.Lets call it Form2. I have a QFrame in Form 2 in which I would like to load Form1.
I did some reading and I found that I could right click on QFrame and choose promote to. I put the base class as QFrame. Promoted class name as Form1. Header file as form1.h.
I get errors now saying:
Form1 has no member named 'setFrameShape'
Form1 has no member named 'setFrameShadow'
I changed the base class as QWidget. And it still did not load the Form1 in the QFrame of Form2
Any help is appreciated.
EDIT:
I used base class as QFrame and
I commented out following lines in the ui_form2.h and it worked frame->setFrameShape() and frame->setFrameShadow() and it worked.
If there is better way to do it kindly let me know

Promoting a widget in designer is just a way of saying "I know this looks like <Standard Widget>, but when you generate code I actually want you to create a <More Specialized Widget> here instead." The widget you promote (and the base class for promotion) need to be of a type that is an ancestor of the type you are promoting to.
In your specific case, the base for your promoted widget should be QWidget (instead of QFrame), because that is the type of Form1. You should put a plain QWidget inside your frame and promote that widget instead of the frame.

Related

Pass arguments to constructor of widget within QStackedWidget

I have a graphical application written in C++ using Qt for an embedded device, which uses a QStackedWidget holding a number of UI widgets. The UI is all designed in Qt Creator's designer tool. When the user navigates through the device's application the display to be shown at that menu option is selected from the QStackedWidget and this all works great.
I'm now wanting to pass in a pointer to some configuration which is read from file when the application starts, but I can't seem to find a way to pass this pointer as an argument into the constructor of a widget on the QStackedWidget. Can anyone help?
My current approach is to call a function I've written within the class of a widget on the QStackedWidget, which works but doesn't feel the best way to do it.
To my knowledge if you want to use custom constructors - with other kinds of arguments than just the QWidget * parent - you have to create the ui programmatically:
create your custom StackedWidget with a special constructor,
prepare the global interface using the designer,
then add the StackedWidget in the constructor of the class after the setupUi method.
The other way is to use an initialization method after the construction of the item, like you did.

Is is possible to edit an individual Widget in the QtDesginer?

I got a external library, which includes a derived class from QGLWidget, very similar to that one here. In that library I have a class:
class PictureGLWidget : public QGLWidget { //.. }
This extends Qt's native QGLWidget and personalizes it. But it was not written by me, I just got it, via a *.dll. So then, I bind that Widget manually in my code to a layout like:
QGridLayout* layout = new QGridLayout;
layout->addWidget(myPictureGLWidget, 0, 1);
ui->verticalLayout_5->addLayout(layout);
since I designed my MainWindowWidget with the integrated QtDesigner, which is by the way very comfortable, I would like to handle my myPictureGLWidget also in the QtDesigner, since I am currently redesigning the MainWindow.
Is there a way doing that? Thnx in advance!
Qt Designer supports any foreign widget class without needing to provide plugins for that. You only have to accept that the widget's properties and appearance won't be available within Designer.
Insert a dummy QWidget into the layout.
Right click on the widget, select "Promote to...".
Add PictureGLWidget as a new class promoted from QWidget. Specify appropriate header files etc.
Promote your widget to PictureGLWidget.
When this is done, the code generated by uic will instantiate a PictureGLWidget where you need it, instead of a dummy QWidget.
If you want to use the PictureGLWidget in the designer instead of a dummy widget, you can write a designer plugin that wraps the widget and exposes it in the widget pallette, provides property support, etc.
I might have misunderstood your question but don't you just add a QGLWidget to your design in Designer. Right click the widget and select Promote to... ?

How to use my treeview subclass in qt designer?

I have a class myTreeView which is a subclass of QTreeView, which I am using in other widget and doing layout manually. now I want to include myTreeView in the new widget using designer so that I can avoid layout code. any suggestions/reference, how to do this ?
Place a QTreeView into your layout in Qt Designer. Right click the QTreeView, click Promote to... add a New Promoted Class definition using the form at the bottom of the dialog.
i.e. specify the base class of your derived class as QTreeView, give the widget a name, and specify where Qt Design can find the header file for your derived class.
That should allow you, at a minimum, to place your widget on the form as you lay it out. It will most likely show up as a grey empty box (much like a QWidget) on the layout however when you compile and build a project using your .ui file your widget will appear.

How to use subclassed class in qt

I've subclassed qt's pushbutton and now I would like to use it in my project (instead of qpushbutton) - the problem I have is that even if I add #include "mybutton.h" in ui_class it gets overwritten and I don't know what else can I do it.
Is there a way to have this button within designer on a panel just like the ordinary qpushbutton is?
Never modify the ui_Class file yourself. Any change you make there will be overwritten when the .ui gets compiled. Instead use the promote to functionality within QtDesigner.
If some forms must be designed, but certain custom widgets are
unavailble to the designer, we can substitute similar widgets to
represent the missing widgets. For example, we might represent
instances of a custom push button class, MyPushButton, with instances
of QPushButton and promote these to MyPushButton so that uic generates
suitable code for this missing class.

Qt: How to connect class to custom Qt Designer Widget

Maybe I'm thinking about this completely wrong...
I've created a new widget in Qt Creator with a Designer file (I picked the Widget template, which generated a source and header file for my custom widget class, and also a designer file).
I then designed the widget with the Designer. I can now create instances of this widget and it will show up in my app.
But it's not terribly useful because I don't know how to customize the widget at runtime.
Let's say all I've got in the widget is a Label and a Button. At runtime, how can I change the text of this label? I can't figure out how to connect the designer stuff to my actual class, and I can't find any documentation on how to do this. Am I missing something?
Thanks!
A few things:
In designer, each of your widgets (the QPushButton, and the QLabel in your case) has a name assigned to it. This name is the name of the variable that you can use in C++ to reference that widget and call functions on it.
Depending on how your custom widget was implemented, you will be able to reference these variables using one of two methods:
If your class inherits from Ui::MyCustomwidget, then your variables are simply member variables of your class and can be accessed at any time (myLabel->setText())
If you have a member variable (generally named ui, of type Ui::MyCustomWidget), then you can access your widgets using the ui object (ui->myLabel->setText())

Resources