5

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?

Tjaart
  • 3,912
  • 2
  • 37
  • 61
  • Isn't this considered a bad practice (access UI elements from c++)? I've been trying myself to learn how to connect my c++ object method to a qml object signal... – johnildergleidisson Jun 04 '13 at 01:20
  • @JoaoMilasch yes it is. This was just an example. The primary goal was to get the C++ code to react to the button click. – Tjaart Jun 04 '13 at 08:16

1 Answers1

8

You should surround the object name with quotation marks:

Button {
    objectName: "btnLogin"
    ...
    ...
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Nishant Shah
  • 1,590
  • 1
  • 15
  • 25