Home > Mobile >  Fail connection objects created in QThread with main thread and fail connection beetwen objects in o
Fail connection objects created in QThread with main thread and fail connection beetwen objects in o

Time:11-11

An object of class someClass some1 in main is created and registered in the qml context. This class creates a class someClass2Var = new someClass2 (); which is pushed onto the thread someClass2Var-> moveToThread (& someThread);. In this class someClass2Var during the run (run) inside the thread someThread two objects are created: someClass4Var = new someClass4 (); and someClass3Var = new someClass3 (); and the signalVarCreated () signal is emitted; to make connect in the class someClass connect (this, & someClass :: signalsomeClass3fromSomeClass, someClass2Var.someClass3Var, & someClass3 :: signalsomeClass3) ;.

When calling the signal signalsomeClass3fromSomeClass from qml, the signal should be called someClass3 :: signalsomeClass3 which in turn calls the slot someClass4 :: slotsomeClass4 - but it doesn't. Why?

How to implement communication from the main thread with objects created in another thread? This is the first question.

Second question: the classes someClass3 and someClass4 are in the same thread someThread, in the class someClass3 a timer is created when the call completes which is emitted the signalsomeClass3 () signal which is associated with the slot someClass4 :: slotsomeClass4, but the call also does not occur. Why? How to implement? Is it possible to?

This version of work is considered for the application to work for Android, there is a project that works under Windows, when another service is called in Android, the application goes to stop, my idea is to transfer the necessary classes to a separate thread, which, as practice has shown, does not stop, unlike the main thread , in the current project everything has been implemented, so I would like to do it as much as possible to use it, the implementation described above would be a solution ..

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    someClass some1;
    qmlRegisterType<someClass>("FuelCore", 1, 0, "SomeClass");

    engine.rootContext()->setContextProperty("MySomeClass", &some1);
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    engine.load(url);
    return app.exec();
}

//---------------------------------------------------------

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include <QObject>
#include <QThread>
#include "someclass2.h"
#include "someclass3.h"
#include "someclass4.h"

class someClass : public QObject
{
    Q_OBJECT
public:
    explicit someClass(QObject *parent = nullptr);
someClass2* someClass2Var;
QThread someThread;
signals:
    void signalSomeClass();
    void signalsomeClass3fromSomeClass();

};

#endif // SOMECLASS_H

//------------------------------------------------------------
#include "someclass.h"
someClass::someClass(QObject *parent) : QObject(parent)
{
    someClass2Var = new someClass2();
    connect(&someClass2Var, &someClass2::signalVarCreated, this, [this](){
        connect(this,&someClass::signalsomeClass3fromSomeClass,someClass2Var.someClass3Var,&someClass3::signalsomeClass3);
    });  
    connect(&someThread, &QThread::started, someClass2Var, &someClass2::run);

    connect(this,&someClass::signalsomeClass3fromSomeClass,someClass2Var,&someClass2::signalsomeClass3_2);

    someClass2Var->moveToThread(&someThread);
    someClass2Var->setRunning(true);
    someThread.start();
}
//----------------------------------------------------
#ifndef SOMECLASS2_H
#define SOMECLASS2_H

#include <QObject>
#include "someclass3.h"
#include "someclass4.h"
#include "someclass.h"

class someClass;
class someClass2 : public QObject
{
    Q_OBJECT
public:
    bool m_running;
    explicit someClass2(QObject *parent = nullptr);
    bool running() const;
    void run();
    int count;
    void setRunning(bool running);
    someClass3* someClass3Var;
    someClass4* someClass4Var;
    someClass *someParent;

signals:
void signalVarCreated();
void signalsomeClass3_2();
};
#endif // SOMECLASS2_H
//-------------------------------------------
#include "someclass2.h"

#include <QDebug>
#include <QThread>

someClass2::someClass2(QObject *parent) : QObject(parent)
{

}
bool someClass2::running() const
{
    return m_running;
}
void someClass2::run()
{
    someClass4Var= new someClass4();
    someClass3Var= new someClass3();
  connect(someClass3Var,&someClass3::signalsomeClass3,someClass4Var,&someClass4::slotsomeClass4);
    signalVarCreated();

    connect(this,&someClass2::signalsomeClass3_2,someClass4Var,&someClass4::slotsomeClass4);
    while (m_running)
    {
        count  ;
        QThread::sleep (1);
    }
}
void someClass2::setRunning(bool running)
{
    if (m_running == running)
        return;

    m_running = running;
}
//----------------------------------------------------
#ifndef SOMECLASS3_H
#define SOMECLASS3_H

#include <QObject>
#include <QTimer>

class someClass3 : public QObject
{
    Q_OBJECT
public:
    explicit someClass3(QObject *parent = nullptr);
    QTimer* tim;

signals:
    void signalsomeClass3();
};
#endif // SOMECLASS3_H
//---------------------------------------------------
#include "someclass3.h"

#include <QThread>

#include <QDebug>
#include <QTimer>

someClass3::someClass3(QObject *parent) : QObject(parent)
{
tim = new QTimer();
tim->start(5000);
connect(tim, &QTimer::timeout, this, [this](){emit signalsomeClass3();});
}
//--------------------------------------------------
#ifndef SOMECLASS4_H
#define SOMECLASS4_H
#include <QObject>
#include <QDebug>

class someClass4 : public QObject
{
    Q_OBJECT
public:
    explicit someClass4(QObject *parent = nullptr);
signals:
public slots:
    void slotsomeClass4() {qDebug()<<"voidslotsomeClass4";};

};

#endif // SOMECLASS4_H
//------------------------------------------------------------
Window {
    visible: true
    width: 640
    height: 480
    Button {
        text: "Call someClass4 slot of object in QThread"
        onClicked: {
            MySomeClass.signalsomeClass3fromSomeClass()
        }
    }
}

CodePudding user response:

Why do you call "signalsomeClass3fromSomeClass()" from qml? And you mark as a signal, i think it must be rather slot please check this https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

CodePudding user response:

Setting the following line in the manifest

meta-data android: name = "android.app.background_running" android: value = "true"

was the solution to my problem of going to stop when overlapping the activity of a Qt application running on Android. Therefore, the need to do the above manipulations has disappeared.

  • Related