处理多个QML之间的信号
Handling signals across several QMLs
我刚开始学习Qt编程,在制作一个简单的测试应用程序时,发现自己在信号和插槽机制方面很吃力。
我有两个QML文件:main.QML和garge.QML.
我有一个管理应用程序的信号和插槽的头文件:
应用程序管理员.h
#ifndef APPLICATIONMANAGER_H
#define APPLICATIONMANAGER_H
#include <QObject>
#include <QDebug>
class ApplicationManager : public QObject
{
Q_OBJECT
public:
explicit ApplicationManager(QObject *parent = 0);
signals:
void newGroceryItem();
public slots:
void addGroceryItem();
};
#endif // APPLICATIONMANAGER_H
用户在main.qml上启动应用程序,按下按钮时加载杂货店.qml
在garge.qml上,我有一个用户可以点击的图像,并发出信号newGroceryItem()
MouseArea {
id: okBtnClkd
anchors.fill: parent
Connections {
onClicked: {
newGroceryItem()
}
}
}
主应用程序main.cpp连接信号,但表示找不到信号newGroceryItem(),因为它不属于main.qml。因此,我的问题是,如何从辅助qml文件garge.qml建立到插槽addGroceryElement()的连接?
编辑:根据要求,以下是main.cpp的内容
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlContext>
#include "applicationmanager.h"
int main(int argc, char *argv[]){
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
ApplicationManager applicationManager;
QObject::connect(window, SIGNAL(newGroceryItem()), &applicationManager, SLOT(addGroceryItem()));
return app.exec();
}
和garge.qml的内容
import QtQuick 2.5
import QtQuick.Controls 1.4
ApplicationWindow {
//Declaration of signals.
signal newGroceryItem()
width: 300
height: 400
visible: true
TextField {
id: product
x: 14
y: 111
width: 270
height: 22
}
Text {
id: text1
x: 14
y: 81
width: 124
height: 24
text: qsTr("Product")
font.pixelSize: 20
}
Image {
id: okBtn
x: 99
y: 290
width: 100
height: 100
source: "img/main/okBtn.png"
MouseArea {
id: okBtnClkd
anchors.fill: parent
Connections {
onClicked: {
newGroceryItem()
}
}
}
}
}
任何QObject
派生类型的方法都可以从QML代码访问,如果它是:
- 用
Q_INVOKABLE
宏标记的公共方法 - 作为公共Qt SLOT的方法
因此有两种方法可以访问addGroceryItem()
。第一种是使用Q_INVOKABLE
宏,如下所示:
class ApplicationManager : public QObject
{
Q_OBJECT
public:
explicit ApplicationManager(QObject *parent = 0);
Q_INVOKABLE void addGroceryItem();
};
第二种方法是使用public slots
,如下所示:
class ApplicationManager : public QObject
{
Q_OBJECT
public:
explicit ApplicationManager(QObject *parent = 0);
public slots:
void addGroceryItem();
};
如果您在C++中创建类对象,那么您应该将该对象设置为main.qml
的上下文数据,如下所示:
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QQmlEngine engine;
ApplicationManager app;
engine.rootContext()->setContextProperty("applicationManager", &app);
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
component.create();
return app.exec();
}
现在,您可以从main.qml
访问在main.cpp
中创建的ApplicationManager
对象,如下所示:
MouseArea {
id: okBtnClkd
anchors.fill: parent
onClicked: {
applicationManager.addGroceryItem();
}
}
有关更多信息,请参阅此处。
相关文章:
- 线程之间的布尔停止信号
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 父进程和子进程之间的 POSIX 信号量
- 在信号槽机制与传统环路之间做出决定
- C/C++ 在条件和阻止呼叫之间收到信号情报
- 使用两个任务之间的静音信号
- 如何将信号连接到2个共享库之间的插槽
- 在QT C 中使用QMOVIE和信号/插槽之间在GIF中导航
- 连接类实例和另一个类实例中的对话框之间的槽和信号
- 不同类别的 2 个对象之间的信号/插槽
- 两个对象QT之间的示例插槽/信号
- 与不同父母的班级中的对象之间发送信号
- QT和Libevent中的信号和插槽之间的差异
- 为什么我的数据无法在信号/插槽的表格之间传输
- 在带有QMovie的GIF动画和Qt中的信号/插槽之间切换
- 在Qt中的线程之间发送对象作为信号参数
- 信号和插槽之间的2个窗口QT
- 尝试在Qt中连接隐藏信号的两种方式之间做出决定
- 无法在多个 Qthread的 QThread 和 finish() 信号之间连接
- 锁、内存屏障、信号量之间的区别