gpt4 book ai didi

c++ - QMetaType::Float 不在 QVariant::Type 中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:27 25 4
gpt4 key购买 nike

我有一个运行良好的应用程序,但它不是在打开警告的情况下编译的。我正在尝试将其重新打开并整理它们,但没有关于如何解决此问题的想法。我有:

QVariant someVar     
QVariant::Type variantType = someVar.type();
switch (variantType) {
case QMetaType::QString:
doSomething1();
break;
case QMetaType::Float:
doSomething2();
break;
}

并收到此警告/错误:

error: case value ‘135’ not in enumerated type ‘QVariant::Type’ [-Werror=switch]

在 QMetaType::Float 行中。我检查了 QT 文档并将 QMetaType::Float 列为值 38。这可能是什么原因造成的?

我能找到的最接近的是 https://github.com/qbittorrent/qBittorrent/issues/2510有同样的错误。有人遇到过这个吗?

最佳答案

Qt 对这两个枚举(QMetaType::TypeQVariant::Type)玩了一些肮脏的把戏。在 QVariant::type() 上引用 4.8.4 文档:

Returns the storage type of the value stored in the variant. Although this function is declared as returning QVariant::Type, the return value should be interpreted as QMetaType::Type. In particular, QVariant::UserType is returned here only if the value is equal or greater than QMetaType::User.

Note that return values in the ranges QVariant::Char through QVariant::RegExp and QVariant::Font through QVariant::Transform correspond to the values in the ranges QMetaType::QChar through QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.

Also note that the types void*, long, short, unsigned long, unsigned short, unsigned char, float, QObject*, and QWidget* are represented in QMetaType::Type but not in QVariant::Type, and they can be returned by this function. However, they are considered to be user defined types when tested against QVariant::Type.

换句话说,函数QVariant::type()返回 QMetaType::Type 的值输入为 QVariant::Type ,并且这两个枚举共享很多(但不是全部)它们的枚举数。这使得在严格的类型系统中处理它们变得困难——它们基本上是摇摆不定的打字机。

在您的例子中,请注意枚举器 QMetaType::Float属于那些QVariant::Type中有直接等价物的那些.

我想说消除警告的最好方法是更改​​ variantTypeQMetaType::Type ,可能会在初始化时进行强制转换和/或在必要时引用 Qt 文档的注释。

关于c++ - QMetaType::Float 不在 QVariant::Type 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31290606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com