how to duplicate widget
-
wrote on 15 May 2017, 02:59 last edited by
I am making a mastodon client application with Qt.
Now I designed one toot (tweet) frame in design mode,
I'd like to duplicate this while executing the program and change the contents (text of labels such as the content of the toot mainly) and display it.Is this possible?
Is there any other way to dynamically create a widget by a program?
-
I am making a mastodon client application with Qt.
Now I designed one toot (tweet) frame in design mode,
I'd like to duplicate this while executing the program and change the contents (text of labels such as the content of the toot mainly) and display it.Is this possible?
Is there any other way to dynamically create a widget by a program?
@wabisuke2718 Widgets (and all classes derived from QObject) cannot be copied.
"Is there any other way to dynamically create a widget by a program?" - well, just doQWidget widget = new QWidget(this);
To "duplicate" set the same properties on the new widget as in the old.
-
wrote on 15 May 2017, 05:13 last edited by
@jsulm So is there no way to duplicate complex QFrames like the next image created in design mode at run time?
If not, how can I dynamically generate and place this QFrame programmatically? -
@jsulm So is there no way to duplicate complex QFrames like the next image created in design mode at run time?
If not, how can I dynamically generate and place this QFrame programmatically?@wabisuke2718 You can design a widget with all the stuff you need inside it in Designer and then create as many instances of this widget as you need.
In QtCreator go to "File/New File or Project.../Qt Designer Form Class".If you want to do this completely at runtime (without using designer) - you can do this as well. Just create your QFrame and all the other widgets. Set the QFrame as parent in those widgets and use a layout in QFrame to position the widgets.
-
@jsulm So is there no way to duplicate complex QFrames like the next image created in design mode at run time?
If not, how can I dynamically generate and place this QFrame programmatically?QWidget widget; Ui::WidgetForm form; // Setup the widget with the form form.setupUi(&widget); // Connect or modify things here if necessary
You should modify the above so to suit your needs. The form's widgets are available as pointers in the
form
variable. -
@wabisuke2718 You can design a widget with all the stuff you need inside it in Designer and then create as many instances of this widget as you need.
In QtCreator go to "File/New File or Project.../Qt Designer Form Class".If you want to do this completely at runtime (without using designer) - you can do this as well. Just create your QFrame and all the other widgets. Set the QFrame as parent in those widgets and use a layout in QFrame to position the widgets.
wrote on 15 May 2017, 06:58 last edited by@jsulm Could you please tell me a little more?
-
@jsulm Could you please tell me a little more?
@wabisuke2718 What exactly?
-
@wabisuke2718 What exactly?
wrote on 15 May 2017, 07:06 last edited byand then create as many instances of this widget as you need.
Specifically, what kind of code will create widgets?
I am sorry for troubling you, please tell me. -
and then create as many instances of this widget as you need.
Specifically, what kind of code will create widgets?
I am sorry for troubling you, please tell me.@wabisuke2718 A widget is just a C++ class so you create an instance as any other:
MyWidget *widget = new MyWidget(parent);
-
wrote on 15 May 2017, 12:06 last edited by
@wabisuke2718
You can always take a look in the .ui file which QDesigner will generate. You can see how it builds the widgets in code and adapt the code in your own coded widget if you want to. -
- So is there no way to duplicate complex QFrames like the next image created in design mode at run time?
Hi, what ever u draw in Designer also exists as code.
If you go to the form and look int SetupUI() function, you will see
all the code that creates such a Frame.You can then easy make it a function and just call that to create more of them.
Copy code to other file as the ui_xx file is generated. -
Hi,
Is this widget meant to show each entry listed in your mastodon feed ?
-
wrote on 16 May 2017, 07:07 last edited by
@SGaist yes
-
In that case, it will likely make more sense to use a QListView with a custom QStyledItemDelegate and a custom model to hold your data. Otherwise you'll end up with thousands of widgets which is not the best performance-wise for showing a list with basically static content.
-
- So is there no way to duplicate complex QFrames like the next image created in design mode at run time?
Hi, what ever u draw in Designer also exists as code.
If you go to the form and look int SetupUI() function, you will see
all the code that creates such a Frame.You can then easy make it a function and just call that to create more of them.
Copy code to other file as the ui_xx file is generated. -
@mrjj That is a good idea.Please tell me how we can change duplicate ui_x.h and ui_y.h files at run time for two UI of same class?
-
@mrjj That is a good idea.Please tell me how we can change duplicate ui_x.h and ui_y.h files at run time for two UI of same class?
-
wrote on 19 Oct 2020, 12:40 last edited by JonB
@Ayush92
For anyone reading this/tempted to answer, please note that the OP is asking the same question in https://forum.qt.io/topic/119689/gui-from-xml-in-c [where I have tried to answer]. We don't want to keep answering the same issues in multiple threads....