QObject::setProperty() always return false
-
I defined a User class derived from QObject
@#ifndef USER_H
#define USER_H#include <string>
using namespace std;#endif // USER_H
class User : public QObject{
Q_OBJECT
Q_PROPERTY(string name READ getName WRITE setName)
Q_PROPERTY(string mobile)
public:string getName(){return this->m_name;} void setName(const string &_name){this->m_name = _name;} string m_name;
};
@then I use setProperty() to set "name" property
@User *user = new User();
if(!user->setProperty("name", "aaa")){
QMessageBox::information(NULL, "setproperty failed", "failed");
}@but setProperty() always return false...
Any suggestion is appreciated:)
-
Hi,
I am not sure it will solve it, but your read accessor should be declared const and your property should be in the private section.
-
Thanks for your reply, I modifed code as you said, but unfortunatelly the same problem still exists..
-
I read the "doc":http://qt-project.org/doc/qt-4.8/properties.html#qt-s-property-system a second time: properties with a custom type (std::string is a custom type for Qt) must be registered with Q_DECLARE_METATYPE to be used with QVariant (see this "section":http://qt-project.org/doc/qt-4.8/properties.html#properties-and-custom-types). That could be your problem.
-
As tilsitt has alredy mentioned you will have to declare std::string as a metatype (so it can be used with QVariant).
@
class User : public QObject{
Q_PROPERTY(string name READ getName WRITE setName)
...
};
Q_DECLARE_METATYPE(string)
@
Be aware that string and std::string are two different types (to the metatype system). So it is either <code>Q_PROPERTY(string name ...)</code> and <code>Q_DECLARE_METATYPE(string)</code> or <code>Q_PROPERTY(std::string name ...)</code> and <code>Q_DECLARE_METATYPE(std::string)</code>. You cannot mix both. -
Why not use "QString" instead of "string"?
-
[quote author="tilsitt" date="1358517715"]I read the "doc":http://qt-project.org/doc/qt-4.8/properties.html#qt-s-property-system a second time: properties with a custom type (std::string is a custom type for Qt) must be registered with Q_DECLARE_METATYPE to be used with QVariant (see this "section":http://qt-project.org/doc/qt-4.8/properties.html#properties-and-custom-types). That could be your problem.[/quote]
Thanks a lot!
As you said, custom type "string" is the problem. I tried Q_DECLARE_METATYPE, it still return false. But after using QString instead of "string", it works now!
My boss is a little hurry, so I decide to use QString -
[quote author="Lukas Geyer" date="1358517907"]As tilsitt has alredy mentioned you will have to declare std::string as a metatype (so it can be used with QVariant).
@
class User : public QObject{
Q_PROPERTY(string name READ getName WRITE setName)
...
};
Q_DECLARE_METATYPE(string)
@
Be aware that string and std::string are two different types (to the metatype system). So it is either <code>Q_PROPERTY(string name ...)</code> and <code>Q_DECLARE_METATYPE(string)</code> or <code>Q_PROPERTY(std::string name ...)</code> and <code>Q_DECLARE_METATYPE(std::string)</code>. You cannot mix both.[/quote][quote author="Code_ReaQtor" date="1358519157"]Why not use "QString" instead of "string"?
[/quote]
thanks all, using QString works.
(Q_DECLARE_METATYPE(string) still let QObject::setProperty return false. But my boss is a little hurry, so I decide to use QString)