PDA

View Full Version : How to use Qt property



bibhukalyana
11th December 2014, 14:30
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 :


#include <QObject>

class MyProperty : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)

public:
MyProperty(QObject *parent = 0);
~MyProperty(){}

QString name();
void setName(QString name);

private:
QString m_name;
};


myproperty.cpp :


MyProperty::MyProperty(QObject *parent)
: QObject(parent)
{
}

QString MyProperty::name()
{
return m_name;
}

void MyProperty::setName(QString name)
{
m_name = name;
}


Property list:


MyProperty obj;
const QList<QByteArray> pList = obj.dynamicPropertyNames();
qDebug()<<pList.size(); // size is 0


main.cpp:


QApplication a(argc, argv);

MainWindow w;
w.show();

return a.exec();


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

thanks.

Lesiok
11th December 2014, 15:00
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.

atomic
11th December 2014, 15:19
Maybe you can use generic functions
QObject::setProperty() and
QObject::property()



QApplication app( argc, argv );
MainWindow w;
w.show();

w.setProperty( "color", "blue" );

QList< QByteArray > x = w.dynamicPropertyNames();
int count = x.size();

qDebug()<< count; // return 1

QVariant v = w.property( "color" );
qDebug()<< v.toString(); // return "blue"


return app.exec();

bibhukalyana
11th December 2014, 16:44
Thanks for reply.
I think I have to use meta object to get the property count and name.

wysota
11th December 2014, 16:56
Why do you want to get the property count and name? What exactly are you trying to achieve?

bibhukalyana
11th December 2014, 17:13
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.

wysota
11th December 2014, 19:33
Why not use QVariantMap instead of QObject?

Lesiok
11th December 2014, 20:14
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.

atomic
11th December 2014, 21:39
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



QPushButton *button = new QPushButton;
QObject *object = button;

button->setDown(true);
object->setProperty("down", true);




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-5/properties.html

bibhukalyana
12th December 2014, 06:12
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::property. 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.

wysota
12th December 2014, 08:26
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.

bibhukalyana
12th December 2014, 11:12
Thanks.
One last query : Which one will perform faster for only reading and writing of values ? Any idea ?

wysota
12th December 2014, 11:44
Definitely QVariant based data structures will be faster than QObject-based ones.