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?


  • Moderators

    @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?

    alt text


  • Moderators

    @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.


  • Qt Champions 2016

    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.



  • @jsulm Could you please tell me a little more?


  • Moderators

    @wabisuke2718 What exactly?



  • @jsulm

    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.


  • Moderators

    @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.


  • Qt Champions 2016

    • 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.


  • Lifetime Qt Champion

    Hi,

    Is this widget meant to show each entry listed in your mastodon feed ?



  • @SGaist yes


  • Lifetime Qt Champion

    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.


Log in to reply