Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to duplicate widget
Forum Update on Monday, May 27th 2025

how to duplicate widget

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 9 Posters 20.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wabisuke2718
    wrote on 15 May 2017, 02:59 last edited by
    #1

    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?

    J 1 Reply Last reply 15 May 2017, 04:26
    0
    • W wabisuke2718
      15 May 2017, 02:59

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 15 May 2017, 04:26 last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • W Offline
        W Offline
        wabisuke2718
        wrote on 15 May 2017, 05:13 last edited by
        #3

        @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

        J K 2 Replies Last reply 15 May 2017, 05:19
        0
        • W wabisuke2718
          15 May 2017, 05:13

          @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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 15 May 2017, 05:19 last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          W 1 Reply Last reply 15 May 2017, 06:58
          2
          • W wabisuke2718
            15 May 2017, 05:13

            @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

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 15 May 2017, 05:27 last edited by kshegunov
            #5
            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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • J jsulm
              15 May 2017, 05:19

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

              W Offline
              W Offline
              wabisuke2718
              wrote on 15 May 2017, 06:58 last edited by
              #6

              @jsulm Could you please tell me a little more?

              J 1 Reply Last reply 15 May 2017, 06:59
              0
              • W wabisuke2718
                15 May 2017, 06:58

                @jsulm Could you please tell me a little more?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 15 May 2017, 06:59 last edited by
                #7

                @wabisuke2718 What exactly?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                W 1 Reply Last reply 15 May 2017, 07:06
                0
                • J jsulm
                  15 May 2017, 06:59

                  @wabisuke2718 What exactly?

                  W Offline
                  W Offline
                  wabisuke2718
                  wrote on 15 May 2017, 07:06 last edited by
                  #8

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

                  J 1 Reply Last reply 15 May 2017, 07:08
                  0
                  • W wabisuke2718
                    15 May 2017, 07:06

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

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 15 May 2017, 07:08 last edited by
                    #9

                    @wabisuke2718 A widget is just a C++ class so you create an instance as any other:

                    MyWidget *widget = new MyWidget(parent);
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kenchan
                      wrote on 15 May 2017, 12:06 last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 15 May 2017, 13:21 last edited by
                        #11
                        • 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.

                        Ayush92A 1 Reply Last reply 19 Oct 2020, 12:12
                        2
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 15 May 2017, 20:52 last edited by
                          #12

                          Hi,

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          W 1 Reply Last reply 16 May 2017, 07:07
                          0
                          • SGaistS SGaist
                            15 May 2017, 20:52

                            Hi,

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

                            W Offline
                            W Offline
                            wabisuke2718
                            wrote on 16 May 2017, 07:07 last edited by
                            #13

                            @SGaist yes

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 16 May 2017, 07:12 last edited by
                              #14

                              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.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • mrjjM mrjj
                                15 May 2017, 13:21
                                • 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.

                                Ayush92A Offline
                                Ayush92A Offline
                                Ayush92
                                wrote on 19 Oct 2020, 12:12 last edited by Ayush92
                                #15

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

                                Pl45m4P Ayush92A 2 Replies Last reply 19 Oct 2020, 12:18
                                0
                                • Ayush92A Ayush92
                                  19 Oct 2020, 12:12

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

                                  Pl45m4P Offline
                                  Pl45m4P Offline
                                  Pl45m4
                                  wrote on 19 Oct 2020, 12:18 last edited by
                                  #16

                                  @Ayush92

                                  Why you need to do that?
                                  Just create multiple instances of the same class?!


                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  1 Reply Last reply
                                  1
                                  • Ayush92A Ayush92
                                    19 Oct 2020, 12:12

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

                                    Ayush92A Offline
                                    Ayush92A Offline
                                    Ayush92
                                    wrote on 19 Oct 2020, 12:39 last edited by
                                    #17

                                    My agenda is this [https://stackoverflow.com/questions/64370459/want-to-change-orientation-from-landscape-to-portrait-view-of-qt-c-application?noredirect=1#comment113829091_64370459].

                                    JonBJ 1 Reply Last reply 19 Oct 2020, 12:40
                                    0
                                    • Ayush92A Ayush92
                                      19 Oct 2020, 12:39

                                      My agenda is this [https://stackoverflow.com/questions/64370459/want-to-change-orientation-from-landscape-to-portrait-view-of-qt-c-application?noredirect=1#comment113829091_64370459].

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on 19 Oct 2020, 12:40 last edited by JonB
                                      #18

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

                                      1 Reply Last reply
                                      1

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved