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 doQWidget 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
form
variable. -
@wabisuke2718 What exactly?
-
@wabisuke2718 A widget is just a C++ class so you create an instance as any other:
MyWidget *widget = new MyWidget(parent);
-
@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 ?
-
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
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....