Gumis
1st December 2016, 23:19
Hello everyone,
I have encountered on problem of signal&slots. When I used QObject::connect function in main, where QGuiApplication is declared, everything works properly. However, when I put the code responsible for finding object from QML and handling the connection on outside class, the funcionality doesn't work. Please for help ;)
Not working code:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include <QQmlProperty>
#include "fun.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
fun f;
f.start();
return app.exec();
}
Source of the class
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include <QQmlProperty>
#include "fun.h"
fun::fun()
{
}
void fun::start()
{
QQmlApplicationEngine engine;
QQmlComponent componentMainWindow(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << componentMainWindow.errors();
QObject *objectMainWindow = componentMainWindow.create();
QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
objectKwad = objectKwad->findChild<QObject*>("kwad");
if (objectKwad) {
qDebug() << "Object was found";
} else {
qDebug() << "Object with given name was not found";
}
objectKwad->setProperty("width", 120);
connect(objectKwad, SIGNAL(buttonClick()), this, SLOT(cppSlot()));
}
Header of the class
#ifndef FUN_H
#define FUN_H
#include <QObject>
#include <QDebug>
class fun : public QObject
{
Q_OBJECT
public:
fun();
void start();
public slots:
inline void cppSlot()
{
qDebug() << "Button was clicked";
}
};
#endif // FUN_H
Working code, when I use the QObject::connect in main.
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include <QQmlProperty>
#include "fun.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
/*fun f;
f.start();*/
QQmlApplicationEngine engine;
QQmlComponent componentMainWindow(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << componentMainWindow.errors();
QObject *objectMainWindow = componentMainWindow.create();
QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
objectKwad = objectKwad->findChild<QObject*>("kwad");
if (objectKwad) {
} else {
qDebug() << "Object with given name was not found";
}
objectKwad->setProperty("width", 120);
QObject::connect(objectKwad, SIGNAL(buttonClick()), &f, SLOT(cppSlot()));
return app.exec();
}
I have encountered on problem of signal&slots. When I used QObject::connect function in main, where QGuiApplication is declared, everything works properly. However, when I put the code responsible for finding object from QML and handling the connection on outside class, the funcionality doesn't work. Please for help ;)
Not working code:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include <QQmlProperty>
#include "fun.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
fun f;
f.start();
return app.exec();
}
Source of the class
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include <QQmlProperty>
#include "fun.h"
fun::fun()
{
}
void fun::start()
{
QQmlApplicationEngine engine;
QQmlComponent componentMainWindow(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << componentMainWindow.errors();
QObject *objectMainWindow = componentMainWindow.create();
QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
objectKwad = objectKwad->findChild<QObject*>("kwad");
if (objectKwad) {
qDebug() << "Object was found";
} else {
qDebug() << "Object with given name was not found";
}
objectKwad->setProperty("width", 120);
connect(objectKwad, SIGNAL(buttonClick()), this, SLOT(cppSlot()));
}
Header of the class
#ifndef FUN_H
#define FUN_H
#include <QObject>
#include <QDebug>
class fun : public QObject
{
Q_OBJECT
public:
fun();
void start();
public slots:
inline void cppSlot()
{
qDebug() << "Button was clicked";
}
};
#endif // FUN_H
Working code, when I use the QObject::connect in main.
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QDebug>
#include <QObject>
#include <QQmlProperty>
#include "fun.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
/*fun f;
f.start();*/
QQmlApplicationEngine engine;
QQmlComponent componentMainWindow(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
qDebug() << componentMainWindow.errors();
QObject *objectMainWindow = componentMainWindow.create();
QObject *objectKwad = objectMainWindow->findChild<QObject*>("kwadlo");
objectKwad = objectKwad->findChild<QObject*>("kwad");
if (objectKwad) {
} else {
qDebug() << "Object with given name was not found";
}
objectKwad->setProperty("width", 120);
QObject::connect(objectKwad, SIGNAL(buttonClick()), &f, SLOT(cppSlot()));
return app.exec();
}