I have written a small console application that prints something on QTimer timeout. I have a counter, which emits a signal to stop the QTimer when it reaches a predetermined value. Everything looks fine, except the application output says crashed when I close the console.
// main.cpp
#include <QCoreApplication>
#include <QDebug>
#include "worker.h"
#include <QThread>
#include <QTimer>
#include <QPointer>
int main(int argc, char *argv[])
{
Worker * worker = new Worker;
// I tried QPointer<Worker> worker = new Worker; also but still gives the same application output
QCoreApplication::connect(worker,
SIGNAL(stopThread
()), worker,
SLOT(deleteLater
()));
timer.start(1000);
return a.exec();
}
// main.cpp
#include <QCoreApplication>
#include <QDebug>
#include "worker.h"
#include <QThread>
#include <QTimer>
#include <QPointer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTimer timer;
Worker * worker = new Worker;
// I tried QPointer<Worker> worker = new Worker; also but still gives the same application output
QCoreApplication::connect(&timer, SIGNAL(timeout()), worker, SLOT(process()));
QCoreApplication::connect(worker, SIGNAL(stopThread()), &timer, SLOT(stop()));
QCoreApplication::connect(worker, SIGNAL(stopThread()), worker, SLOT(deleteLater()));
timer.start(1000);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
//worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
{
Q_OBJECT
int nCounter;
public:
explicit Worker
(QObject *parent
= 0);
~Worker();
signals:
void stopThread(void);
public slots:
void process(void);
};
#endif // WORKER_H
//worker.h
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker : public QObject
{
Q_OBJECT
int nCounter;
public:
explicit Worker(QObject *parent = 0);
~Worker();
signals:
void stopThread(void);
public slots:
void process(void);
};
#endif // WORKER_H
To copy to clipboard, switch view to plain text mode
// worker.cpp
#include "worker.h"
#include <QDebug>
#include <QTime>
#include <QThread>
nCounter(0)
{}
Worker::~Worker()
{
}
void Worker::process(void)
{
nCounter++;
qDebug
() <<
QTime::currentTime().
toString("hh:mm:ss");
if (nCounter == 10)
{
qDebug() << "-x-x-x- Count Reached 10 -x-x-x-";
emit stopThread();
}
}
// worker.cpp
#include "worker.h"
#include <QDebug>
#include <QTime>
#include <QThread>
Worker::Worker(QObject *parent) : QObject(parent),
nCounter(0)
{}
Worker::~Worker()
{
}
void Worker::process(void)
{
nCounter++;
qDebug() << QTime::currentTime().toString("hh:mm:ss");
if (nCounter == 10)
{
qDebug() << "-x-x-x- Count Reached 10 -x-x-x-";
emit stopThread();
}
}
To copy to clipboard, switch view to plain text mode
Kindly help me identify the why the message says the application crashes.
Starting /home/rahul/Documents/build-qonsole-Desktop_Qt_5_4_1_GCC_32bit-Debug/qonsole...
/home/rahul/Documents/build-qonsole-Desktop_Qt_5_4_1_GCC_32bit-Debug/qonsole crashed
Starting /home/rahul/Documents/build-qonsole-Desktop_Qt_5_4_1_GCC_32bit-Debug/qonsole...
/home/rahul/Documents/build-qonsole-Desktop_Qt_5_4_1_GCC_32bit-Debug/qonsole crashed
To copy to clipboard, switch view to plain text mode
Bookmarks