Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect qml with c++ using signal ans lot
Forum Update on Monday, May 27th 2025

Connect qml with c++ using signal ans lot

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vibolvireak
    wrote on last edited by
    #1

    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

    MainWindows.h

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

    Main.cpp

    #include <mainwindow.h>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <qobject.h>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        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 :)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @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()));
      
      1 Reply Last reply
      1
      • V Offline
        V Offline
        vibolvireak
        wrote on last edited by
        #3

        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.

        mrjjM 1 Reply Last reply
        0
        • V vibolvireak

          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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @vibolvireak said:
          hi
          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) :
          QMainWindow(parent),
          ui(new Ui::MainWindow) {}

          V 1 Reply Last reply
          0
          • mrjjM mrjj

            @vibolvireak said:
            hi
            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) :
            QMainWindow(parent),
            ui(new Ui::MainWindow) {}

            V Offline
            V Offline
            vibolvireak
            wrote on last edited by vibolvireak
            #5

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

            mrjjM 1 Reply Last reply
            0
            • V vibolvireak

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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?

              mrjjM 1 Reply Last reply
              0
              • mrjjM mrjj

                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?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

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

                Im no expert on QML, but the examples here
                http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#connecting-to-qml-signals
                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.

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

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi
                  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?

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vibolvireak
                    wrote on last edited by
                    #9

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

                    mrjjM 1 Reply Last reply
                    0
                    • V vibolvireak

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @vibolvireak
                      oh, that was the missing piece :)
                      QQmlContext
                      Happy coding :)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved