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 clone QWidget ?
Forum Updated to NodeBB v4.3 + New Features

How to clone QWidget ?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 9.5k Views 1 Watching
  • 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1
    Form *form = new Form;
    ......
    Form *form1 = new Form;
    form1 = form;

    https://github.com/sonichy

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      In general. You can't. There is no one-size-fit-all copy solution for QObjects. You are free however to define a public: void clone(Form* other); inside Form and implement your custom clone operation

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      sonichyS 1 Reply Last reply
      3
      • VRoninV VRonin

        In general. You can't. There is no one-size-fit-all copy solution for QObjects. You are free however to define a public: void clone(Form* other); inside Form and implement your custom clone operation

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @VRonin How to write clone operation inside QWidget ? It seems have to return a QWidget point in QWidget of itself.

        https://github.com/sonichy

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BinC
          wrote on last edited by
          #4

          Hi, Sonichy, before you write "clone" or "copy" for a QObject, maybe take a look at QObject document section "No Copy Constructor or Assignment Operator" and "Qt Objects: Identity vs Value" on the Qt Object Model page.

          1 Reply Last reply
          3
          • F Offline
            F Offline
            FloatFlower.Huang
            wrote on last edited by FloatFlower.Huang
            #5

            @sonichy create a class inherit QWidget.

            class Form : public QWidget
            {
              Q_OBEJCT
            public:
              Form(QObject* parent = 0) {
                // Constructor
              }
            
              // ... whatever
            
              void clone(Form* other) {
            
                // clone data of "other" to "this"
            
              }
            
            }
            

            And you can do this:

            Form form1 = new Form();
            Form form2 = new Form();
            
            form1.copy(form2);
            

            Further more, you can free to call functions of QWidget, and you can use QWidget pointer point to Form objects because of polymorphism.

            As the comment above, Qt design objects as identity. Although the two objects we copy have same value, they have different identities, so just implement a copy function to copy the value, and just copy the value to wherever you want.

            VRoninV 1 Reply Last reply
            1
            • F FloatFlower.Huang

              @sonichy create a class inherit QWidget.

              class Form : public QWidget
              {
                Q_OBEJCT
              public:
                Form(QObject* parent = 0) {
                  // Constructor
                }
              
                // ... whatever
              
                void clone(Form* other) {
              
                  // clone data of "other" to "this"
              
                }
              
              }
              

              And you can do this:

              Form form1 = new Form();
              Form form2 = new Form();
              
              form1.copy(form2);
              

              Further more, you can free to call functions of QWidget, and you can use QWidget pointer point to Form objects because of polymorphism.

              As the comment above, Qt design objects as identity. Although the two objects we copy have same value, they have different identities, so just implement a copy function to copy the value, and just copy the value to wherever you want.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @FloatFlower.Huang said in How to clone QWidget ?:

              form1 = form2; // with operator overloaded.

              Wrong, that's a pointer assignment.
              Since operator= for const reference is deleted I would not make it confusing defining one for a const pointer. the clone method is more than enough

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              F 1 Reply Last reply
              4
              • VRoninV VRonin

                @FloatFlower.Huang said in How to clone QWidget ?:

                form1 = form2; // with operator overloaded.

                Wrong, that's a pointer assignment.
                Since operator= for const reference is deleted I would not make it confusing defining one for a const pointer. the clone method is more than enough

                F Offline
                F Offline
                FloatFlower.Huang
                wrote on last edited by
                #7

                @VRonin That my fault, sorry, I will remove the code.
                Thanks for your reminder.

                1 Reply Last reply
                0
                • sonichyS Offline
                  sonichyS Offline
                  sonichy
                  wrote on last edited by
                  #8

                  There is no way to be lazy, I have to set the data one by one...

                  Form *form1 = new Form;
                  form1->ui->labelFilename->setText(form->ui->labelFilename->text());
                  form1->ui->labelSize->setText(form->ui->labelSize->text());
                  form1->ui->progressBar->setValue(form->ui->progressBar->value());
                  form1->ui->labelSpeed->setText(form->ui->labelSpeed->text());
                  form1->ui->labelURL->setText(form->ui->labelURL->text());
                  form1->ui->labelElapse->setText(form->ui->labelElapse->text());
                  form1->ui->labelTimeCreate->setText(form->ui->labelTimeCreate->text());
                  form1->ui->labelPath->setText(form->ui->labelPath->text());

                  https://github.com/sonichy

                  JonBJ 1 Reply Last reply
                  2
                  • sonichyS sonichy

                    There is no way to be lazy, I have to set the data one by one...

                    Form *form1 = new Form;
                    form1->ui->labelFilename->setText(form->ui->labelFilename->text());
                    form1->ui->labelSize->setText(form->ui->labelSize->text());
                    form1->ui->progressBar->setValue(form->ui->progressBar->value());
                    form1->ui->labelSpeed->setText(form->ui->labelSpeed->text());
                    form1->ui->labelURL->setText(form->ui->labelURL->text());
                    form1->ui->labelElapse->setText(form->ui->labelElapse->text());
                    form1->ui->labelTimeCreate->setText(form->ui->labelTimeCreate->text());
                    form1->ui->labelPath->setText(form->ui->labelPath->text());
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @sonichy Is that a question or a statement? Yes, to copy/clone you will have to do so member-by-member. Which would likely be a lot for a whole form/error-prone/unmaintainable/not possible for some widgets.... You really need to make a complete copy of a whole "form", do you?

                    1 Reply Last reply
                    0

                    • Login

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