English 中文(简体)
如何核实QVariant类型:UserType的类型?
原标题:How to verify QVariant of type QVariant::UserType is expected type?
  • 时间:2010-07-07 08:54:50
  •  标签:
  • qt
  • qvariant

I m写作测试法将自动复制所有Q_PROPERTY的植被,有些特性正在使用通过qRegisterMetaType注册的类型。 如果想要读/做这些内容,就需要使用QVariant: 用户Type将其储存成变量。 迄今情况良好。

但是,如果想测试这些特性的写法,也需要知道其类型。 对于已经是标准类型的缺陷,即可以通过QVariant来做到这一点:类型()但作为用户类型的一个位置,如何做到这一点?

从QVariant一带发现:

<编码>bool QVariant:canConvert (类型t)

但是,如果在遗体情况下会导致错误的类型,那么一小小小小小小小小小小小点怀疑吗?

因此,核查QVariant储存哪类用户的无节制方法是什么?

最佳回答

用户定义类型:。 QVariant:userType()。 它的工作如QVariant: 类型,但回归定义的用户类型分类,而QVariant:类型()总是返回QVariant:UserType。

http://doc.qt.io/qt-5/qvariant.html QVariant: 类型Name(),将类型名称作为示意图。

EDIT:

这很可能取决于你如何确定QVariant。 直接使用 QVariant:QVariant(int category, const void *拷贝)

我有三类:

class MyFirstType
{ 
    public:
        MyFirstType();
        MyFirstType(const MyFirstType &other);
        ~MyFirstType();

        MyFirstType(const QString &content);

        QString content() const;

    private:
        QString m_content;
};
Q_DECLARE_METATYPE(MyFirstType);

第三个没有Q_DECLARE_METATYPE

I 储存在QVariant:

 QString content = "Test";

 MyFirstType first(content);

 MySecondType second(content);

 MyThirdType third(content);

 QVariant firstVariant;
 firstVariant.setValue(first);

 QVariant secondVariant = QVariant::fromValue(second);

 int myType = qRegisterMetaType<MyThirdType>("MyThirdType");

 QVariant thirdVariant(myType, &third); // Here the type isn t checked against the data passed

 qDebug() << "typeName for first :" << firstVariant.typeName();
 qDebug() << "UserType :" << firstVariant.userType();
 qDebug() << "Type : " << firstVariant.type();

 [...]

我收到:

typeName for first : MyFirstType 
UserType : 256 
Type :  QVariant::UserType 

typeName for second : MySecondType 
UserType : 257 
Type :  QVariant::UserType 

typeName for third : MyThirdType 
UserType : 258 
Type :  QVariant::UserType 
问题回答

暂无回答




相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签