Important: Please read the Qt Code of Conduct -

Connect qml with c++ using signal ans lot

  • Hello, i have try on google and found out a code to connect qml with c++ signal and slot. But i got error on qml debug
    QObject::connect: Cannot connect (null)::clicked() to (null)::LoginClick()

    my code is


    #ifndef MAINWINDOW
    #define MAINWINDOW
    #include <QMainWindow>
    class MainWindow : public QMainWindow {
        MainWindow(QWidget *parent = 0);
    public slots:
        void LoginClick();
    #endif // MAINWINDOW


    #include <mainwindow.h>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <qobject.h>
    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
        QObject *bt = engine.findChild<QObject*>("btLogin");
        QObject::connect(bt, SIGNAL(clicked()), 0, SLOT(LoginClick()));
        return app.exec();
    void MainWindow::LoginClick(){

    Qml file

     Button {
            x: 270
            y: 223
            width: 100
            height: 35
            objectName: "btLogin"
            text: qsTr("Click")

    I'm appreciated for your help :)

  • Lifetime Qt Champion

    @vibolvireak said:

    QObject::connect(bt, SIGNAL(clicked()), 0, SLOT(LoginClick()));

    It seems that bt is NULL ? so maybe findChild don't find it?
    also, the last part of connect is wrong

    Object::connect(bt, SIGNAL(clicked()), 0, SLOT(LoginClick()));

    as the 0 should be pointer to mainwindow as LoginClick() lives there
    so you you must have an instance of MainWindow and give pointer to connect.

    something like:

    MainWindow w;
     QObject::connect(bt, SIGNAL(clicked()), &w, SLOT(LoginClick()));

  • First of all. I understand what you gonna say. And first i try MainWindow w; as your suggest and i got the other error

    main.cpp:17: error: undefined reference to `MainWindow::MainWindow(QWidget*)'

    This time i don't know what is going .... Yes... for me c++ i'm not good at all Sorry about that.

  • Lifetime Qt Champion

    @vibolvireak said:
    did u define the body for
    MainWindow(QWidget *parent = 0); ?

    in the cpp file?

    it sounds like it says you didn't.

    something like
    MainWindow::MainWindow(QWidget* parent) :
    ui(new Ui::MainWindow) {}

  • @mrjj Oh yes i forgot the body. It seem really hard and messy for me but i try..

  • Lifetime Qt Champion

    yeah for one "click" its a lot of code :)
    Is there a reason you want to mix QML and c++ since c++ is not really your thing?

  • Lifetime Qt Champion

    where did u see the engine.findChild example?
    as Im not sure it can work.

    Im no expert on QML, but the examples here
    uses a QQuickView to connect signals.

    So do wonder if QQmlApplicationEngine will also create them as findable object as
    a view type would.
    Just asking.

    this will find the button
    QObject *rootObject = engine.rootObjects().first();
    qDebug() << " found " << rootObject;
    QObject::connect(rootObject, SIGNAL(clicked()),this, SLOT(LoginClick()));

  • Lifetime Qt Champion

    using engine.rootObjects().first(); it seems to accept the connect but
    you dont seem to show the QML?
    or you just did not show that part?

  • Sorry for late with reply.. Finally i found and easy way with QQmlContext :) I'm thank for your help XD really happy

  • Lifetime Qt Champion

    oh, that was the missing piece :)
    Happy coding :)

Log in to reply