1

I am trying to connect a c++ signal to QML but i cannot figure out how to do this exactly.

i have read the following questions:

C++ SIGNAL to QML SLOT in Qt

Warning when connecting c++ signal to qml slot

and a few others. But i am still missing something as i am getting the following error:

QObject::connect: Cannot connect DataGather::loginSequenceFinished() to (null)::testing123()

my main.cpp is:

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 )
            }
        }
    }

What i am trying to accomplish is:

When the Loginbutton is clicked, the function LoginAndFetchID is run which takes care of the whole login procedure. And when it finishes, i want to emit a signal to qml so that the next procedure can be started.. for example, showing a load screen if login information is correct.

So the question is: What am i missing with this connection thing? How do i fix this error?

i probably have to specify the full path to testing123 because it says: to (null)::testing123().

but i have no idea what the full path would be.

Community
  • 1
  • 1
CantThinkOfAnything
  • 1,129
  • 1
  • 15
  • 40
  • 1
    Have you tried http://stackoverflow.com/questions/22953866/sending-a-signal-to-a-qml-item-from-c-qt5 ? – Kakadu Apr 03 '15 at 10:52
  • Thank you, yesterday i used that link for figuring this out but today i couldnt find it again – CantThinkOfAnything Apr 03 '15 at 12:41
  • although i might add that its very important declare the source file first. After that, grab the child to connect it. This was my problem because i did it the other way around which obviously does not work. – CantThinkOfAnything Apr 03 '15 at 12:43

0 Answers0