For non Qt code I am trying to call Qcoreapplication with help of another thread.
Here the code:
ApplicationThread.h
#pragma once
#include <qthread.h>
#include "squidstat_dll_global.h"
#include <iostream>
class SQUIDSTAT_DLLSHARED_EXPORT ApplicationThread:public QThread
{
void run();
};
class SQUIDSTAT_DLLSHARED_EXPORT ApplicationStarter : public QObject
{
Q_OBJECT
public:
ApplicationStarter();
~ApplicationStarter();
public slots:
void onStarted();
};
ApplicationThread.cpp
#include "ApplicationThread.h"
#include "MainWindow.h"
#include "qcoreapplication.h"
namespace ToolThreadGlobal
{
static int argc = 1;
static char* argv[] = { QString("SquidState.exe").toLocal8Bit().data(),NULL};
static QCoreApplication *coreApp = nullptr;
static ApplicationThread *toolThread = nullptr;
};
void ApplicationThread::run() {
if (ToolThreadGlobal::coreApp){
std::cout << "\nApplicationThread: thread run method is call";
ToolThreadGlobal::coreApp->exec();
}
}
ApplicationStarter::ApplicationStarter()
{
if (ToolThreadGlobal::toolThread == nullptr)
{
std::cout << "\nApplicationstarter Constructor call";
ToolThreadGlobal::toolThread = new ApplicationThread();
connect(ToolThreadGlobal::toolThread, &ApplicationThread::started, this, &ApplicationStarter::onStarted, Qt::DirectConnection);
ToolThreadGlobal::toolThread->start();
}
}
ApplicationStarter::~ApplicationStarter()
{
std::cout << "\nApplicationStarter:: destructor is called";
// Ensure that the thread and the QCoreApplication are cleanly shut down:
ToolThreadGlobal::toolThread->quit();
ToolThreadGlobal::coreApp->quit();
ToolThreadGlobal::coreApp->deleteLater();
ToolThreadGlobal::coreApp = nullptr;
ToolThreadGlobal::toolThread->terminate();
ToolThreadGlobal::toolThread->wait();
ToolThreadGlobal::toolThread->deleteLater();
ToolThreadGlobal::toolThread = nullptr;
}
void ApplicationStarter::onStarted()
{
std::cout << "\nApplicationStarter::signal received";
if (QCoreApplication::instance() == NULL) {
std::cout << "\nApplicationStarter::call QcoreApplication";
ToolThreadGlobal::coreApp = new QCoreApplication(ToolThreadGlobal::argc, ToolThreadGlobal::argv);
std::cout << "\nApplicationStarter::call worker";
MainWindow w;
std::cout << "\nApplicationStarter::call before exe";
}
}
SquidState.h
#pragma once
#include "squidstat_dll_global.h"
#include "ApplicationThread.h"
#ifndef _SQUIDSTAT_H_
#define _SQUIDSTAT_H_
namespace AISquid {
class SQUIDSTAT_DLLSHARED_EXPORT SquidStat {
public:
SquidStat();
~SquidStat();
void startAPP();
void closeAPP();
};
}
#endif // !_SQUIDSTAT_H_
SquidState.cpp
#include <stdlib.h>
#include "SquidStat.h"
#include "ApplicationThread.h"
AISquid::SquidStat::SquidStat() {
std::cout << "\nSquidStat: SquidStat class object is created";
};
AISquid::SquidStat::~SquidStat() {
std::cout << "\nSquidStat: SquidStat class object is destriod ";
};
void AISquid::SquidStat::startAPP()
{
};
void AISquid::SquidStat::closeAPP()
{
};
Testing My dll with help of below code.
tester.cpp
#include "SquidStat.h"
#include "ApplicationThread.h"
void main(int argc, char *argv[])
{
ApplicationStarter start;
AISquid::SquidStat obj;
obj.startAPP();
return;
}
OutPut of Tester:
0_1554857416616_285cd514-f416-4d15-817b-51334702ce84-image.png
What is wrong am I doing here? I am receiving call for slot onStarted(); once ApplicationStarter start; object is destroy.
Any suggestion would be more appreciable.