Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
how to duplicate widget
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 do
QWidget widget = new QWidget(this);
To "duplicate" set the same properties on the new widget as in the old.
@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.
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
@jsulm Could you please tell me a little more?
@wabisuke2718 What exactly?
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);
kenchan last edited by
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.
Is this widget meant to show each entry listed in your mastodon feed ?
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.
Ayush92 last edited by Ayush92
@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?
Pl45m4 last edited by
Why you need to do that?
Just create multiple instances of the same class?!
Ayush92 last edited by
JonB last edited by JonB
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....