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

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


  • Qt Champions 2016

    @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.


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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?


  • Qt Champions 2016

    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()));


  • Qt Champions 2016

    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?



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


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.