如何使用自定义Qt类型与QML信号? [英] How to use a custom Qt type with a QML signal?
问题描述
我在我的Qt 5.2 qml应用程序中创建了一个自定义类型
类设置:public QObject
{
Q_OBJECT
Q_PROPERTY(QString key READ key WRITE setKey)
Q_PROPERTY(QVariant value READ value WRITE setValue)
public:
设置(QObject * parent = 0);
QString key()const;
void setKey(QString const& key);
QVariant value()const;
void setValue(QVariant const& value);
private:
QString m_key;
QVariant m_value;
};
并在我的主函数中注册:
int main(int argc,char * argv [])
{
QApplication app(argc,argv)
qmlRegisterType< Setting>(Setting,1,0,Setting);
// ...
}
我想有一个按钮,其中有一个信号与我的自定义类型作为参数:
import设置1.0
Button
{
signal settingChanged(string,Setting)
// ...
}
每次按下按钮,信号都会发出。
我的qml信号与我的C ++槽
QObject :: connect(myButton,SIGNAL(settingChanged(QString,Setting) SLOT(settingChanged(QString,Setting)));
Qt说
code> QObject :: connect:没有这样的信号Button_QMLTYPE_41_QML_45 :: settingChanged(QString,Setting)in ...
如果我从信号定义中删除我的自定义类型
按钮
{
信号settingChanged(string)
// ...
}
并修改连接调用
QObject :: connect(myButton,SIGNAL(settingChanged(QString)),this,SLOT (QString)));
连接调用工作。
可以在我的应用程序的qml部分中使用我的自定义qml类型设置。
谢谢!
在我仔细阅读了Qt文档后,我在这里找到了答案。
http:// qt-project。 org / doc / qt-5 / qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals
文档说
当使用QML对象类型作为信号参数时,参数
应该使用var作为类型,并且应该使用QVariant类型在C ++中接收该值
。
因此,
设置
的qml描述中的信号定义必须替换为var
。import设置1.0
按钮
{
信号settingChanged(string,var)
// ...
}
要连接qml信号与C ++槽,代码必须
Object :: connect(myButton,SIGNAL(settingChanged(QString,QVariant) ),this,SLOT(settingChanged(QString,QVariant)));
此时连接工作正常!
为了使整个事情运行,
QVariant
必须被投射到插槽内的正确类型。在我的情况下,一个设置
类型。void Foo :: settingChanged (QString name,QVariant const& var)
{
Setting * setting = qobject_cast< Setting *>(var.value< QObject *>
// ...
}
I've created a custom type inside my Qt 5.2 qml application
class Setting : public QObject { Q_OBJECT Q_PROPERTY(QString key READ key WRITE setKey) Q_PROPERTY(QVariant value READ value WRITE setValue) public: Setting(QObject * parent = 0); QString key() const; void setKey(QString const & key); QVariant value() const; void setValue(QVariant const & value); private: QString m_key; QVariant m_value; };
and registered it in my main function:
int main(int argc, char *argv[]) { QApplication app(argc, argv); qmlRegisterType<Setting>("Setting", 1,0, "Setting"); // ... }
In my application I would like to have a button that has a signal with my custom type as a parameter:
import Setting 1.0 Button { signal settingChanged(string, Setting) // ... }
Every time I hit the button the signal should be emitted.
If I try to connect my qml signal with my C++ slot
QObject::connect(myButton, SIGNAL(settingChanged(QString, Setting)), this, SLOT(settingChanged(QString, Setting)));
Qt says
QObject::connect: No such signal Button_QMLTYPE_41_QML_45::settingChanged(QString, Setting) in ...
If I remove my custom type from the signal definition
Button { signal settingChanged(string) // ... }
and modify the connect call
QObject::connect(myButton, SIGNAL(settingChanged(QString)), this, SLOT(settingChanged(QString)));
the connect call worked.
I could use my custom qml type Setting inside the qml part of my application. But what I do wrong if I would like to use it with my signal settingChanged?
Thanks!
解决方案After I've readed the Qt documentation very carefully, I've found the answer here
The documentation says
When a QML object type is used as a signal parameter, the parameter should use var as the type, and the value should be received in C++ using the QVariant type.
So, the signal definition inside the qml description of
Setting
has to be replaced withvar
.import Setting 1.0 Button { signal settingChanged(string, var) // ... }
To connect the qml signal with the C++ slot, the code has to be
Object::connect(myButton, SIGNAL(settingChanged(QString, QVariant)), this, SLOT(settingChanged(QString, QVariant)));
At this point the connection worked!
To make the whole thing running, the
QVariant
has to be casted to the right type inside the slot. In my case to aSetting
type.void Foo::settingChanged(QString name, QVariant const & var) { Setting * setting = qobject_cast<Setting*>(var.value<QObject*>()); // ... }
这篇关于如何使用自定义Qt类型与QML信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!