Results 1 to 9 of 9

Thread: QObject, properties

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QObject, properties

    I want to ask:
    i created and object type class name Movie:
    Qt Code:
    1. #ifndef MOVIE_H
    2. #define MOVIE_H
    3.  
    4. #include <QObject>
    5.  
    6. QT_BEGIN_NAMESPACE
    7. class QLabel;
    8. QT_END_NAMESPACE
    9.  
    10. class Movie : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. Movie(QObject *parent = 0);
    15. ~Movie();
    16. public slots:
    17. QString name()
    18. {return _name;}
    19. void setName(QString name_in)
    20. {_name = name_in;}
    21.  
    22. QString date()
    23. {return _date;}
    24. void setDate(QString date_in)
    25. {_date = date_in;}
    26.  
    27. QString about()
    28. {return _about;}
    29. void setAbout(QString about_in)
    30. {_about = about_in;}
    31.  
    32. QString picture()
    33. {return _picture;}
    34. void setPicture(QString picture_in)
    35. {_picture = picture_in;}
    36.  
    37. private:
    38. QString _name;
    39. QString _date;
    40. QString _about;
    41. QString _picture;
    42. };
    43.  
    44. #endif // MOVIE_H
    To copy to clipboard, switch view to plain text mode 

    In mine main application i Use the following code:
    Qt Code:
    1. QObject *asd = new Movie;
    2. asd->setName(titleString);
    To copy to clipboard, switch view to plain text mode 

    I get the following error:
    'class QObject' has no member named 'setName'

    What am I doing wrong?

  2. #2
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QObject, properties

    On line 16, why do you have public slots:? Shouldn't it simply be public?

  3. #3
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject, properties

    Quote Originally Posted by Archa4 View Post
    What am I doing wrong?
    you're calling setName() on a QObject. QObject does not have a member called "setName()"

  4. #4
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject, properties

    I found the problem - when i used
    QObject *asd = new Movie;
    i should really had to use:
    Movie *asd = new Movie;

    Now i have another problem:
    i try to make QVector<Movie> movie_list, and it works
    but when i try something like
    movie_list.append(asd);

    I get 4 errors and 20 warnings...
    What am I doing wrong this time?


    Well I solved this also.
    I needed to make the QVector like this:
    QVector<Movie *>;
    Last edited by Archa4; 9th February 2011 at 12:16.

  5. #5
    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, properties

    QVector requires its content to be copyable which is not true for QObject derivatives. That's why you need to use pointers which can be copied.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject, properties

    Then I need to ask another thing related to objects:
    I need to make few of those and then pass QVector of them to another function. What should I do to prevent information loss? (objects are being created inside a cycle, and after it ends i think the information disappears, or does it)?

  7. #7
    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, properties

    All C++principles apply here.

    By the way I don't see anything in your code that would justify making it a QObject. Is there a particular reason for it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject, properties

    Is there a better way to create something like object?

  9. #9
    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, properties

    You can implement a class that is not derived fro QObject. Inheriting QObject makes sense if the class is to benefit from the meta-object system (e.g. it has signals or slots). Your class seems to be a regular data carrier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    Archa4 (11th February 2011)

Similar Threads

  1. dynamic properties
    By Hogwarts in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2010, 16:37
  2. gang together properties
    By khusmann in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2008, 22:23
  3. Replies: 1
    Last Post: 31st October 2007, 14:14
  4. Saving properties of a QObject based class
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2007, 01:26
  5. ViewPort Properties
    By Kapil in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 11:57

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.