Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I react to a QML button click in C++

I am trying to launch a different QML Page from my C++ code by hooking into the clicked() slot of a button in my QML but it's not working.

    Button {
        objectName: btnLogin
        text: qsTr("Login")
        id: btnLogin
    }

And the c++

QObject *newButton = root->findChild<QObject*>("btnLogin");
QObject::connect(newButton, SIGNAL(clicked()), this, SLOT(loginClick()));

The slots in my hpp file:

 public slots:  
    void loginClick();

And my clicked method:

void GConnectBB::loginClick() {
    int i = 0;

    Button *newButton = root->findChild<Button*>("btnLogin");
    if (newButton)
        newButton->setProperty("text", "New button text");
}


QObject *newButton = root->findChild<QObject*>("btnLogin"); 

Is null when I check through the debugger. I am extremely rusty with C++ and completely new to Qt, please be gentle :) What could I be doing wrong?

like image 271
Tjaart Avatar asked Nov 02 '12 08:11

Tjaart


Video Answer


1 Answers

You should surround the object name with quotation marks:

Button {
    objectName: "btnLogin"
    ...
    ...
}
like image 118
Nishant Shah Avatar answered Sep 22 '22 05:09

Nishant Shah