我正在尝试将c ++信号连接到QML,但我无法弄清楚如何准确地执行此操作。
我已阅读以下问题:
Warning when connecting c++ signal to qml slot
和其他一些人。但我仍然遗漏了一些东西,因为我收到以下错误:
QObject::connect: Cannot connect DataGather::loginSequenceFinished() to (null)::testing123()
我的main.cpp是:
int main(int argc, char *argv[])
{
// QGuiApplication app(argc, argv);
QApplication app(argc, argv);
DataGather gather;
//createData();
TreeModel model("test");
//QTreeView treeview;
//treeview.setModel(&model);
//treeview.show();
QQuickView *view = new QQuickView;
view->setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view->rootContext();
qmlRegisterType<TreeModel>();
qmlRegisterType<ListModel>();
qmlRegisterType<DataGather>();
ctxt->setContextProperty("treemodel", &model);
ctxt->setContextProperty("gatherer", &gather);
auto root = view->rootObject();
auto myElement = root->findChild<QObject*>(QLatin1Literal("LoginButtonID"));
QObject::connect(&gather, SIGNAL(loginSequenceFinished()), myElement, SLOT(testing123()));
view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
//view.setSource(QUrl(QStringLiteral("qrc:/FrontPage.qml")));
view->show();
return app.exec();
}
main.qml:
Rectangle {
objectName: "LoginButtonID"
id: loginRectID
height: loginRectText.height
width: loginRectText.width
color: "black"
clip: true
anchors.bottom: parent.bottom
anchors.bottomMargin: parent.height * 0.05
anchors.right: passwordFieldID.right
function testing123() { console.log("working!"); }
Text {
id: loginRectText
font.family: "Segoe UI Light"
font.pointSize: 30
text: "Login"
color: "red"
anchors.right: parent.right
anchors.top: parent.top
}
MouseArea {
id: loginButtonMouseAreaID
width: parent.width + 40
height: parent.height + 40
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
onClicked: {
gatherer.loginAndFetchID(usernameTextID.text, passwordTextID.text)
console.log( usernameTextID.text + " " + passwordTextID.text )
}
}
}
我想要完成的是:
单击“登录”按钮时,将运行LoginAndFetchID函数,该函数负责整个登录过程。当它完成时,我想向qml发出一个信号,以便下一个程序可以启动...例如,如果登录信息是正确的,则显示一个加载屏幕。
所以问题是:这个连接的东西我错过了什么?我该如何解决这个错误?
我可能必须指定测试123的完整路径,因为它说: to(null):: testing123()。
但我不知道完整路径会是什么。