#include <QCoreApplication>
#include <QObject>
#include <QThread>
#include <QDebug>
// Plain function with C linkage declaration
extern "C" {
int multiplyByTwo(int i);
}
// Plain function impl
int multiplyByTwo(int i) {
return i * 2;
}
Q_OBJECT
public:
Worker();
~Worker();
public slots:
void process();
signals:
void finished();
private:
// add your variables here
};
Worker::Worker() { }
Worker::~Worker() { }
// Start processing data.
void Worker::process() {
int value = 1;
while (value < 1000000000) {
qDebug() << value;
value = multiplyByTwo(value);
}
emit finished();
}
int main(int argc, char **argv) {
Worker* worker = new Worker;
worker->moveToThread(thread);
QObject::connect(thread,
SIGNAL(started
()), worker,
SLOT(process
()));
QObject::connect(worker,
SIGNAL(finished
()), thread,
SLOT(quit
()));
QObject::connect(worker,
SIGNAL(finished
()), worker,
SLOT(deleteLater
()));
QObject::connect(thread,
SIGNAL(finished
()), thread,
SLOT(deleteLater
()));
thread->start();
QObject::connect(thread,
SIGNAL(destroyed
()),
qApp,
SLOT(quit
()));
return app.exec();
}
#include "main.moc"
#include <QCoreApplication>
#include <QObject>
#include <QThread>
#include <QDebug>
// Plain function with C linkage declaration
extern "C" {
int multiplyByTwo(int i);
}
// Plain function impl
int multiplyByTwo(int i) {
return i * 2;
}
class Worker : public QObject {
Q_OBJECT
public:
Worker();
~Worker();
public slots:
void process();
signals:
void finished();
private:
// add your variables here
};
Worker::Worker() { }
Worker::~Worker() { }
// Start processing data.
void Worker::process() {
int value = 1;
while (value < 1000000000) {
qDebug() << value;
value = multiplyByTwo(value);
}
emit finished();
}
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QThread* thread = new QThread;
Worker* worker = new Worker;
worker->moveToThread(thread);
QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
QObject::connect(thread, SIGNAL(destroyed()), qApp, SLOT(quit()));
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks