Results 1 to 4 of 4

Thread: Problem with QObject::setProperty

  1. #1
    Join Date
    Feb 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows

    Default Problem with QObject::setProperty

    Hi everyone,

    I'm currently working on a library that defines several types and I want to be able to use them into the Qt's property system.

    For instance, I have a ColorButton class containing a "tulipColor" field whose type is tlp::Color:

    Qt Code:
    1. class TLP_QT_SCOPE ColorButton : public QPushButton {
    2. Q_OBJECT
    3. Q_PROPERTY(Color tulipColor READ tulipColor WRITE setTulipColor)
    4.  
    5. tlp::Color _color;
    6. public:
    7. explicit ColorButton(QWidget *parent = 0);
    8.  
    9. Color tulipColor() const;
    10.  
    11. protected:
    12. void paintEvent(QPaintEvent *);
    13.  
    14. public slots:
    15. void setTulipColor(const Color&);
    16. };
    To copy to clipboard, switch view to plain text mode 

    I declared the tlp::Color type using the Q_DECLARE_METATYPE macro and the qRegisterMetaType function:

    Qt Code:
    1. Q_DECLARE_METATYPE(tlp::Color)
    2. // ........
    3. qRegisterMetaType<tlp::Color>("Color");
    To copy to clipboard, switch view to plain text mode 

    Then I use the following code:
    Qt Code:
    1. QVariant value = QVariant::fromValue<tlp::Color>(tlp::Color(255,0,0));
    2. qWarning() << "User Type for value is: " << value.userType();
    3.  
    4. ColorButton* btn = new ColorButton();
    5. for (int i=0;i<btn->metaObject()->propertyCount();++i) {
    6. QMetaProperty prop = btn->metaObject()->property(i);
    7. qWarning() << "UserType for the " << prop.name() << " property is: " << prop.userType();
    8. }
    9.  
    10. btn->setProperty("tulipColor",QVariant::fromValue<tlp::Color>(Color(255,0,0)));
    11. btn->show();
    To copy to clipboard, switch view to plain text mode 

    And get the following output:

    User Type for value is: 258
    UserType for the tulipColor property is: 258
    So the tulipColor property is correctly registered with a valid userType. But when I try to change the value using the setProperty method, it returns false and my setTulipColor method is never called.

    Please help :/

  2. #2
    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: Problem with QObject::setProperty

    What if you change "Color" to "tlp::Color"?
    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.


  3. #3
    Join Date
    Feb 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QObject::setProperty

    Using namespace names in Q_PROPERT declarations doesn't sem to compile, I get the same error as related in this bug report: https://bugreports.qt.nokia.com//browse/QTBUG-2151

    That's why I tried using Color since my class is in the same namespace. Now if I change qRegisterMetaType<tlp::Color>("Color"); to qRegisterMetaType<tlp::Color>("tlp::Color"); the output of my program becomes:
    User Type for value is: 258
    UserType for the tulipColor property is: 0
    I'm really lost on this one

  4. #4
    Join Date
    Feb 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QObject::setProperty

    Help anyone ?

Similar Threads

  1. Making enums "setProperty" enabled.
    By hickscorp in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2011, 15:46
  2. Replies: 8
    Last Post: 12th November 2009, 00:51
  3. Problem with QObject::timerEvent()
    By ericwny in forum Qt Programming
    Replies: 9
    Last Post: 5th June 2009, 00:31
  4. Problem with Destructor of QObject
    By mikro in forum Qt Programming
    Replies: 5
    Last Post: 9th July 2006, 19:05
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 17:49

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.