Results 1 to 13 of 13

Thread: How to use Qt property

  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to use Qt property

    Hi everyone,
    I want to use qt property(Q_property) for dynamically get/set value in a class. But I am not able to get how to use it.

    myproperty.h :
    Qt Code:
    1. #include <QObject>
    2.  
    3. class MyProperty : public QObject
    4. {
    5. Q_OBJECT
    6. Q_PROPERTY(QString name READ name WRITE setName)
    7.  
    8. public:
    9. MyProperty(QObject *parent = 0);
    10. ~MyProperty(){}
    11.  
    12. QString name();
    13. void setName(QString name);
    14.  
    15. private:
    16. QString m_name;
    17. };
    To copy to clipboard, switch view to plain text mode 

    myproperty.cpp :
    Qt Code:
    1. MyProperty::MyProperty(QObject *parent)
    2. : QObject(parent)
    3. {
    4. }
    5.  
    6. QString MyProperty::name()
    7. {
    8. return m_name;
    9. }
    10.  
    11. void MyProperty::setName(QString name)
    12. {
    13. m_name = name;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Property list:
    Qt Code:
    1. MyProperty obj;
    2. const QList<QByteArray> pList = obj.dynamicPropertyNames();
    3. qDebug()<<pList.size(); // size is 0
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. QApplication a(argc, argv);
    2.  
    3. MainWindow w;
    4. w.show();
    5.  
    6. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    Is it the correct way to use a property ? Please help me.

    thanks.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    Because QObject::dynamicPropertyNames Returns the names of all properties that were dynamically added to the object using setProperty().. Your class don't have dynamic property.

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    Maybe you can use generic functions
    Qt Code:
    1. QObject::setProperty()
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. QObject::property()
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QApplication app( argc, argv );
    2. MainWindow w;
    3. w.show();
    4.  
    5. w.setProperty( "color", "blue" );
    6.  
    7. QList< QByteArray > x = w.dynamicPropertyNames();
    8. int count = x.size();
    9.  
    10. qDebug()<< count; // return 1
    11.  
    12. QVariant v = w.property( "color" );
    13. qDebug()<< v.toString(); // return "blue"
    14.  
    15.  
    16. return app.exec();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    Thanks for reply.
    I think I have to use meta object to get the property count and name.

  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: How to use Qt property

    Why do you want to get the property count and name? What exactly are you trying to achieve?
    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
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    I have a xml. It contains the property name and value. I want to fill the structure/class and want to pass it to a function.
    Basically i want to fill a class without knowing its member variable and type.

  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: How to use Qt property

    Why not use QVariantMap instead of QObject?
    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
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    Or if You need an QObject simply use dynamic properties with methods QObject::setProperty and QObject::property. In this case, you do not need to define anything.

  9. #9
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    I think I have to use meta object to get the property count and name.
    No, you didn't need this in this case.
    In documentation about Qt property system you can find

    Qt Code:
    1. QPushButton *button = new QPushButton;
    2. QObject *object = button;
    3.  
    4. button->setDown(true);
    5. object->setProperty("down", true);
    To copy to clipboard, switch view to plain text mode 

    Accessing a property through its WRITE accessor is the better of the two, because it is faster and gives better diagnostics at compile time, but setting the property this way requires that you know about the class at compile time. Accessing properties by name lets you access classes you don't know about at compile time.
    http://doc.qt.io/qt-4.8/
    http://doc.qt.io/qt-5/properties.html

  10. #10
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    Thanks to all for helping me.
    Why not use QVariantMap instead of QObject?
    I think QHash will be a good option.

    Or if You need an QObject simply use dynamic properties with methods QObject::setProperty and QObject:roperty. In this case, you do not need to define anything.
    This is also fine. I wll try it.

    Accessing a property through its WRITE accessor is the better of the two
    Yes. I think it is the best option. But for adding/removing a field I have to change it in everywhere.

    Can you please tell me which option will be the good one ?

    thanks.

  11. #11
    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: How to use Qt property

    Quote Originally Posted by bibhukalyana View Post
    Can you please tell me which option will be the good one ?
    It depends what you want to do. If you just need a set of key-value pairs then QVariantMap is the simplest approach. If you specifically need anything from QObject legacy (e.g. signals and slots), then you will have to use QObject.
    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.


  12. #12
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use Qt property

    Thanks.
    One last query : Which one will perform faster for only reading and writing of values ? Any idea ?

  13. #13
    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: How to use Qt property

    Definitely QVariant based data structures will be faster than QObject-based ones.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 24th November 2014, 09:28
  2. How to get a stylesheet property?
    By P@u1 in forum Qt Programming
    Replies: 15
    Last Post: 20th September 2011, 21:15
  3. Getting object's property...
    By gilamran in forum Newbie
    Replies: 1
    Last Post: 18th February 2011, 18:07
  4. Help with property browser
    By Jsvc in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2010, 02:49
  5. enum property
    By illuzioner in forum Qt Tools
    Replies: 10
    Last Post: 22nd August 2006, 22:47

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.