Qt:如何在 C++ 端而不是 QML 监控 Q_PROPERTY 更改 [英] Qt : How to monitor a Q_PROPERTY change on C++ side instead of QML
问题描述
我使用的是 Qt 5.9.3.我在我的应用程序的 main.qml
代码:
//main.qml我的快速项目{属性颜色 nextColoronNextColorChanged:{console.log("下一个颜色是:" + nextColor.toString())}}//MyQuickItem.h类 MyQuickItem : 公共 QQuickItem {}
问题:
如何在C++端定义onNextColorChanged
?
我知道我也可以将 nextColor
作为 C++ 类 MyQuickItem
中的一个属性.像这样
//MyQuickItem.h类 MyQuickItem : 公共 QQuickItem {Q_PROPERTY(QColor nextColor READ nextColor WRITE setNextColor NOTIFY nextColorChanged)}
是否可以在MyQuickItem
中监控OnNextColorChanged
?
我们可以使用 QMetaObject 来获取属性和信号,然后我们通过旧式连接:
#ifndef MYQUICKITEM_H#define MYQUICKITEM_H#include #include 类 MyQuickItem : 公共 QQuickItem{Q_OBJECT上市:MyQuickItem(QQuickItem *parent = Q_NULLPTR): QQuickItem(parent){}受保护:void componentComplete(){int index =metaObject()->indexOfProperty("nextColor");const QMetaProperty property = metaObject()->property(index);如果(property.hasNotifySignal()){const QMetaMethod s = property.notifySignal();QString sig = QString("2%1").arg(QString(s.methodSignature()));连接(这个,sig.toStdString().c_str(),这个,SLOT(onNextColorChanged()));}}私人插槽:void onNextColorChanged(){int index =metaObject()->indexOfProperty("nextColor");const QMetaProperty property = metaObject()->property(index);qDebug()<<颜色"<<property.read(this);}};#endif//MYQUICKITEM_H
完整示例可在以下链接中找到.>
I am using Qt 5.9.3. I have following property declared in my app's main.qml
Code:
//main.qml
MyQuickItem {
property color nextColor
onNextColorChanged: {
console.log("The next color will be: " + nextColor.toString())
}
}
// MyQuickItem.h
class MyQuickItem : public QQuickItem {
}
Question:
How can I make onNextColorChanged
be defined in the C++ side?
I know that I can also make nextColor
as a property inside C++ class MyQuickItem
. like so
// MyQuickItem.h
class MyQuickItem : public QQuickItem {
Q_PROPERTY(QColor nextColor READ nextColor WRITE setNextColor NOTIFY nextColorChanged)
}
Is it possible to monitor OnNextColorChanged
inside MyQuickItem
?
We can use the QMetaObject to obtain the property and the signal, then we connect it through the old style:
#ifndef MYQUICKITEM_H
#define MYQUICKITEM_H
#include <QQuickItem>
#include <QDebug>
class MyQuickItem : public QQuickItem
{
Q_OBJECT
public:
MyQuickItem(QQuickItem *parent = Q_NULLPTR): QQuickItem(parent){}
protected:
void componentComplete(){
int index =metaObject()->indexOfProperty("nextColor");
const QMetaProperty property = metaObject()->property(index);
if (property.hasNotifySignal()){
const QMetaMethod s = property.notifySignal();
QString sig = QString("2%1").arg(QString(s.methodSignature()));
connect(this, sig.toStdString().c_str() , this, SLOT(onNextColorChanged()));
}
}
private slots:
void onNextColorChanged(){
int index =metaObject()->indexOfProperty("nextColor");
const QMetaProperty property = metaObject()->property(index);
qDebug()<<"color" << property.read(this);
}
};
#endif // MYQUICKITEM_H
The complete example can be found in the following link.
这篇关于Qt:如何在 C++ 端而不是 QML 监控 Q_PROPERTY 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!