Connect qml with c++ using signal ans lot
-
wrote on 3 Jan 2016, 11:43 last edited by
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 :)
-
Lifetime Qt Championwrote on 3 Jan 2016, 11:54 last edited by mrjj 1 Mar 2016, 11:59
@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 wrongObject::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()));
-
wrote on 3 Jan 2016, 12:06 last edited by
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.
-
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 Championwrote on 3 Jan 2016, 12:09 last edited by mrjj 1 Mar 2016, 12:11@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) {} -
@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) {}wrote on 3 Jan 2016, 12:26 last edited by vibolvireak 1 Mar 2016, 12:27@mrjj Oh yes i forgot the body. It seem really hard and messy for me but i try..
-
@mrjj Oh yes i forgot the body. It seem really hard and messy for me but i try..
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? -
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 Championwrote on 3 Jan 2016, 12:47 last edited by mrjj 1 Mar 2016, 12:56Hi
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())); -
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? -
wrote on 3 Jan 2016, 13:35 last edited by
Sorry for late with reply.. Finally i found and easy way with QQmlContext :) I'm thank for your help XD really happy
-
Sorry for late with reply.. Finally i found and easy way with QQmlContext :) I'm thank for your help XD really happy
@vibolvireak
oh, that was the missing piece :)
QQmlContext
Happy coding :)
1/10