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>
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName
)
public:
~MyProperty(){}
private:
};
#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;
};
To copy to clipboard, switch view to plain text mode
myproperty.cpp :
MyProperty
::MyProperty(QObject *parent
){
}
{
return m_name;
}
void MyProperty
::setName(QString name
) {
m_name = name;
}
MyProperty::MyProperty(QObject *parent)
: QObject(parent)
{
}
QString MyProperty::name()
{
return m_name;
}
void MyProperty::setName(QString name)
{
m_name = name;
}
To copy to clipboard, switch view to plain text mode
Property list:
MyProperty obj;
const QList<QByteArray> pList = obj.dynamicPropertyNames();
qDebug()<<pList.size(); // size is 0
MyProperty obj;
const QList<QByteArray> pList = obj.dynamicPropertyNames();
qDebug()<<pList.size(); // size is 0
To copy to clipboard, switch view to plain text mode
main.cpp:
MainWindow w;
w.show();
return a.exec();
QApplication a(argc, argv);
MainWindow w;
w.show();
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.
Bookmarks