【问题标题】:QVariant::QVariant(Qt::GlobalColor)' is privateQVariant::QVariant(Qt::GlobalColor)' 是私有的
【发布时间】:2013-02-21 17:12:55
【问题描述】:

头文件中的声明

QColor dialogBoja, dialogBoja1;

.cpp 文件

dialogBoja = postavke.value("boja", Qt::black).toString();
//postavke means settings
dialogBoja1 = postavke.value("boja1", Qt::white).toString();

正如我在标题中所说,当我尝试在 Qt5 中编译它时出现错误:QVariant::QVariant(Qt::GlobalColor)' is private

如何解决。

【问题讨论】:

    标签: qt qt5


    【解决方案1】:

    您需要显式创建一个 QColor 对象。这应该有效:

    dialogBoja = postavke.value("boja", QColor(Qt::black)).toString();
    

    标题中解释了这样做的原因:

    // These constructors don't create QVariants of the type associcated
    // with the enum, as expected, but they would create a QVariant of
    // type int with the value of the enum value.
    // Use QVariant v = QColor(Qt::red) instead of QVariant v = Qt::red for
    // example.
    

    【讨论】:

      【解决方案2】:

      看起来他们想将 QVariant 从 QtGui 模块(如 QColor)中分离出来,并在 5.0 中删除了该构造函数。一些语法解释here

      因为 QVariant 是 QtCore 库的一部分,它不能提供 将函数转换为 QtGui 中定义的数据类型,例如 QColor, QImage 和 QPixmap。换句话说,没有 toColor() 函数。 相反,您可以使用 QVariant::value() 或 qvariant_cast() 模板函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-18
        • 1970-01-01
        相关资源
        最近更新 更多