我目前在使用自己的类型作为属性类型时遇到问题。
我的自定义类型是在命名空间中定义的。当我将它放入全局命名空间时,一切正常,没有任何问题。
是否可以在命名空间中使用自定义类型作为属性值?
这是一个最小示例(我使用的是 Qt5.0 和 g++ 4.7.3)
这是test.pro
文件:
LANGUAGE = C++
QT += core gui widgets
TARGET = test
QMAKE_CXXFLAGS += -std=c++11
HEADERS += test.h
SOURCES += test.cpp
这是test.h
文件:
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
namespace MyNamespace
{
struct MyValue
{
private:
QString a;
int b;
public:
MyValue(const QString &a="", int b=0)
: a(a), b(b)
{
}
bool operator !=(const MyValue &other) const
{
return (this->a!=other.a) || (this->b!=other.b);
}
friend QDataStream &operator<<(QDataStream &stream, const MyNamespace::MyValue &value);
friend QDataStream &operator>>(QDataStream &stream, MyNamespace::MyValue &value);
friend QDebug operator<<(QDebug debug, const MyNamespace::MyValue &value);
};
inline QDataStream &operator<<(QDataStream &stream, const MyNamespace::MyValue &value)
{
stream << value.a;
return stream << value.b;
}
inline QDataStream &operator>>(QDataStream &stream, MyNamespace::MyValue &value)
{
stream >> value.a;
return stream >> value.b;
}
inline QDebug operator<<(QDebug debug, const MyNamespace::MyValue &value)
{
return debug << "MyValue("<<value.a<<", "<<value.b<<")";
}
}
Q_DECLARE_METATYPE(MyNamespace::MyValue)
namespace AnotherNamespace
{
typedef MyNamespace::MyValue MyValue;
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(MyValue value READ value WRITE setValue NOTIFY valueChanged)
public:
MyValue value()
{
return value_;
}
public slots:
void setValue(const MyValue &value)
{
if(this->value() != value)
{
value_ = value;
emit valueChanged(value);
}
}
signals:
void valueChanged(const MyValue &value);
private:
MyValue value_;
};
}
这是 test.cpp 文件:
#include "test.h"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
qRegisterMetaTypeStreamOperators<MyNamespace::MyValue>("MyNamespace::MyValue");
AnotherNamespace::MyClass myObject;
myObject.setValue(MyNamespace::MyValue("the answer", 42));
QMetaObject metaObject = AnotherNamespace::MyClass::staticMetaObject;
QMetaProperty metaProperty = metaObject.property(metaObject.indexOfProperty("value"));
QVariant variant = metaProperty.read(&myObject);
MyNamespace::MyValue value = variant.value<MyNamespace::MyValue>();
qDebug() << value;
qDebug() << "\nIt's interesting, that this is working without problems:";
variant = QVariant::fromValue(MyNamespace::MyValue("2^4", 16));
value = variant.value<MyNamespace::MyValue>();
qDebug() << value;
return 0;
}
这个最小示例的输出是
QMetaProperty::read: Unable to handle unregistered datatype 'MyValue' for property 'AnotherNamespace::MyClass::value'
MyValue( "" , 0 )
It's interesting, that this is working without problems:
MyValue( "2^4" , 16 )
如前所述,在从最小示例中删除任何命名空间使用后,一切正常,以下输出没有任何问题
MyValue( "the answer" , 42 )
It's interesting, that this is working without problems:
MyValue( "2^4" , 16 )