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. Deep copy of a struct containing a QWidget*
QtWS25 Last Chance

Deep copy of a struct containing a QWidget*

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 7.8k 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    I have a small struct containing a pointer to a QWidget @ struct Field
    {
    QString name;
    QWidget* widget;
    bool isOn;
    };@ Then I would like to use a QVector<Field>. However when I push_back a new Field(), I get a copy of the QWidget pointer, not the QWidget itself. The common answer to that is to implement a copy constructor and an assignement constructor.
    Both of them should perform a deep copy of the struct.
    However it looks like there is now way to deep copy my QWidget*

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      As you probably know, a copy of a pointer is just a copy of that pointer, not a copy of that what is pointed to. So, if you want a deep copy, use values in your struct instead of pointers. However, with QObject derived classes such as QWidget, you can not do that. They are simply not copyable. So, you are out of luck.

      I am wondering what you are trying to achieve with this design. Why do you want to construct new QWidgets every time you insert or retreive a value from your vector (that's when a copy occurs)? What is that supposed to accomplish? Where are you going to display those widgets?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on last edited by
        #3

        I try to build a user editable list of custom fields.
        This fields could be checkbox, lineedit or combobox, etc.
        You think my design sucks?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          So... basically, you're looking for a property editor?

          There are many examples for those to be found. The way you are going about it, is certainly not going to work though.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JulienMaille
            wrote on last edited by
            #5

            Reinventing the wheel?
            Would you have a link to one of the simple examples you are mentioning? Thanks a lot!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              There are a few property editors to be found in the widgets section of qt-apps.org, but there is also one in Qt Designer that you may be able to leverage. I personally used (and extended) Yape from there.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dialingo
                wrote on last edited by
                #7

                I do not see a problem using and filling QVector<Field>.
                A field element holds a pointer to the widget and assumes ownership of the widget. So you have complete control over the widget. Of course the widget can not be moved from the location where it was created into the QVector, but why would you insist that this happens?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JulienMaille
                  wrote on last edited by
                  #8

                  [quote author="dialingo" date="1310031616"]I do not see a problem using and filling QVector<Field>.[/quote] When you insert a new Field to the QVector @QVector<Field> fields;
                  fields.push_back(Field());@ A Field will be created, then it will be copied in the QVector calling the copy-constructor, and eventually the original Field is deleted. Hence if ~Field() delete the QWidget*, then my QVector contains a FIeld with an invalid pointer to a destroyed QWidget*

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    The destructor of class Field only deletes the widget if you call that in the destructor, e.g. you need this code:

                    @
                    Field::~Field()
                    {
                    delete widget;
                    }
                    @

                    As long as you do not have this, the widget is not destroyed.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JulienMaille
                      wrote on last edited by
                      #10

                      Sure, but I need to destroy the widget or I will have memory leaks.
                      I could keep a list of widgets and call a destructor on it when closing the application, but this does not sound like a nice idea.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #11

                        You could use a [[Doc:QSharedPointer]] for this:

                        bq. QSharedPointer will delete the pointer it is holding when it goes out of scope, provided no other QSharedPointer objects are referencing it.

                        Edit: fixed doc link; Andre

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dialingo
                          wrote on last edited by
                          #12

                          Maybe memory management is much easier.
                          You create a widget like this:
                          @QWidget *widget = new QLineEdit(mainWindow);@
                          Note that the widget is a child widget of the MainWindow
                          @
                          Field field;
                          field.name = "a line edit";
                          field.isOn = true;
                          field.widget = widget;

                          QList<Field> fields; // prefer list over vector
                          fields.push_back(field);
                          @
                          When you pop a field from the list the widget will not be deleted automatically. Implement Volkers suggestion if you need automatic deletion of the widget. Forgetting to delete a widget will not cause a memory leak because all widgets have a parent.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            JulienMaille
                            wrote on last edited by
                            #13

                            Thanks a lot to all of you guys!

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jupiter
                              wrote on last edited by
                              #14

                              you could also use a QList<Field*> or a QVector<Field*> then you dont have to take care about copying. you can implement a destructor to delete the Widget in Field, and delete the field when you dont need it anymore

                              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