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
    #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