【问题标题】:QObject connect QSystemDeviceInfo::Profile to QVariantQObject 将 QSystemDeviceInfo::Profile 连接到 QVariant
【发布时间】:2012-01-29 04:33:45
【问题描述】:

我的应用程序有一个 QML 部分,它需要知道我处于什么状态。currentProfileChanged 函数有一个信号给我一个 QSystemDeviceInfo::Profile,我想将它转换为 QVaraint,以便 QML可以将 profile 理解为 0 到 7 之间的数字,但是这个函数:

QObject::connect(deviceInfo, 
    SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile)),
    rootObject,
    SLOT(changePower(QVariant(QSystemDeviceInfo::Profile))));

给出这个奇怪的错误:

[Qt Message] Object::connect: No such slot
     QDeclarativeItem_QML_3::changePower(QVariant(QSystemDeviceInfo::Profile))  
     in C:/Users/Gerhard/QTProjects/Raker/main.cpp:142

我在这里做错了什么?

如果我试试这个:

QObject::connect(deviceInfo, 
    SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile)),
    rootObject,
    SLOT(changePower(QVariant(QSystemDeviceInfo::Profile))));

上面写着:

[Qt Message] Object::connect: No such slot
     QDeclarativeItem_QML_3::changePower(QSystemDeviceInfo::Profile)  
     in C:/Users/Gerhard/QTProjects/Raker/main.cpp:142

如果我将任何一个或两个都更改为 QVariant,它也会抱怨不兼容的参数。

【问题讨论】:

    标签: c++ qt profile qml signals-slots


    【解决方案1】:

    槽函数的参数不能与信号中的参数不匹配。您的插槽应指定为..SLOT(changePower(QSystemDeviceInfo::Profile))..

    【讨论】:

    • 谢谢,但我已经尝试过了,但它抱怨:[Qt Message] Object::connect: No such slot QDeclarativeItem_QML_3::changePower(QSystemDeviceInfo::Profile) in C:/Users/Gerhard/ QTProjects/Raker/main.cpp:142
    • 不如按照 Arnold Spence 的建议将您的插槽声明从 changePower(QVariant(QSystemDeviceInfo::Profile)) 更改为 changePower(QSystemDeviceInfo::Profile)
    • 它在 qml 中没有声明,它看起来像这样:function changePower(power)。看起来像这样的所有其他方法都采用 QVariant,然后它们就可以工作了。
    • 我没有使用过 QML,所以恐怕我无法提供更多建议。希望有的人可以帮助你。对不起。
    【解决方案2】:

    由于似乎没有更简单的方法,我不得不编写一个函数来转换类型并添加更多信号槽,但至少它现在可以工作,如果你想要的话,这是我的函数:

    #include <QObject>
    #include <QVariant>
    #include <QSystemDeviceInfo>
    #include <QDebug>
    
    using namespace QtMobility;
    
    
    class changeVAriant : public QObject
    {
        Q_OBJECT
    
    public slots:
        void toVariant(QSystemDeviceInfo::Profile prof)
        {
            emit newVariant(QVariant(prof));
        }
    signals:
        void newVariant(QVariant);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2013-12-06
      • 1970-01-01
      • 2013-11-22
      • 2022-01-07
      • 2012-08-20
      • 1970-01-01
      相关资源
      最近更新 更多