Results 1 to 6 of 6

Thread: QObject and copy Constructors

  1. #1
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QObject and copy Constructors

    Hi all,

    I am trying to get my head around the model/view way of programming. I am making a simple example using my own class, and have run into a problem. My own class is very simple at this stage, since I'm just trying to get it to work:

    Qt Code:
    1. class VfAlert : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. VfAlert( QObject *parent = 0 ) : QObject( parent ) {}
    7. VfAlert( const VfAlert &alert );
    8. QString title;
    9. };
    10.  
    11. class VfAlertListModel : public QAbstractListModel
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. VfAlertListModel( const QList<VfAlert> &alerts, QObject *parent = 0 )
    17. : QAbstractListModel(parent), alertList(alerts) {}
    18.  
    19. // int rowCount(const QModelIndex &parent = QModelIndex()) const;
    20. // QVariant data(const QModelIndex &index, int role) const;
    21. // QVariant headerData(int section, Qt::Orientation orientation,
    22. // int role = Qt::DisplayRole) const;
    23.  
    24. private:
    25. QList<VfAlert> alertList;
    26. };
    To copy to clipboard, switch view to plain text mode 

    and the copy constructor for VfAlert:

    Qt Code:
    1. VfAlert::VfAlert( const VfAlert &alert )
    2. {
    3. title = alert.title;
    4.  
    5. };
    To copy to clipboard, switch view to plain text mode 
    I had to add the copy constructor because without it, it wouldn't compile.

    Now it compiles ok, but I get this warning:
    warning: base class 'class QObject' should be explicitly initialized in the copy constructor
    That sounds pretty serious, so I'm guessing I shouldn't ignore it, but can't get my head around what exactly I should be doing with a QObject in the copy constructor

    Any ideas?

    Thanks

  2. #2
    Join Date
    Jun 2008
    Location
    Glenwood, NJ USA
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QObject and copy Constructors

    QObject does not support copying. It's copy constructor is not public.

  3. The following user says thank you to awhite1159 for this useful post:

    December (17th July 2008)

  4. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject and copy Constructors

    And your class does not need to derive from QObject anyway, does it?

    (If you have classes that do have to derive from QObject, you can (must) store pointers (preferably smart ones, imho) in a QList.

    HTH

    PS: To be on the safe side: you must not use smart pointers like boost::shared_ptr for objects that are owned by another QObject (parent*). Otherwise it might get deleted twice... which causes quite a bit of fun.

  5. The following user says thank you to caduel for this useful post:

    December (17th July 2008)

  6. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject and copy Constructors

    Your copy constructor doesn't do anything an implicit copy constructor wouldn't do, by the way, so you might not implement it at all (of course if QObject allowed copying, which it does not).

  7. The following 2 users say thank you to wysota for this useful post:

    alizadeh91 (16th June 2013), December (17th July 2008)

  8. #5
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QObject and copy Constructors

    Thanks for all the responses. the easy solution was not to dervice from QObject. I had planned to use some signals in that vlass, but can re-do the logic to go without.

    I didn't know QObjects don't have public copy constructors, never run across that before.

    My copy constructor will do lots more later (this was just to get the concept working, not the actual class that will be in the final code) so I can't really do without it.

    Got rid of the QObject and it's fine now

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject and copy Constructors

    Quote Originally Posted by December View Post
    Thanks for all the responses. the easy solution was not to dervice from QObject. I had planned to use some signals in that vlass, but can re-do the logic to go without.
    You can change the design from isA to hasA (I mean have a member variable that is a [pointer to] QObject).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.