Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------------------------------------------------------------
- test.pro
- QT -= gui
- QT += core
- QT += xml
- #CONFIG += c++11 console
- CONFIG += console
- CONFIG -= app_bundle
- # The following define makes your compiler emit warnings if you use
- # any Qt feature that has been marked deprecated (the exact warnings
- # depend on your compiler). Please consult the documentation of the
- # deprecated API in order to know how to port your code away from it.
- DEFINES += QT_DEPRECATED_WARNINGS
- # You can also make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- # You can also select to disable deprecated APIs only up to a certain version of Qt.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
- SOURCES += myclass.cpp
- SOURCES += worker.cpp
- SOURCES += main.cpp
- # Default rules for deployment.
- # qnx: target.path = /tmp/$${TARGET}/bin
- # else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
- HEADERS += \
- myclass.h \
- worker.h
- -------------------------------------------------------------------------------------------------------------------
- main.cpp
- #include <QCoreApplication>
- #include <QDebug>
- #include <myclass.h>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- myclass MyClass;
- MyClass.init();
- int crexec = a.exec();
- return crexec;
- }
- -------------------------------------------------------------------------------------------------------------------
- myclass.cpp
- #include "myclass.h"
- #include "worker.h"
- myclass::myclass(QObject *parent) : QObject(parent),
- mWorker(nullptr)
- {
- }
- void myclass::init()
- {
- QThread* thread = new QThread;
- worker *mWorker = new worker();
- mWorker->moveToThread(thread);
- connect(thread, &QThread::started, mWorker, &worker::loop, Qt::DirectConnection);
- connect(mWorker, &worker::finished, thread, &QThread::quit, Qt::DirectConnection);
- mWorker->thread()->start();
- }
- void myclass::stop()
- {
- mWorker->stop();
- }
- -------------------------------------------------------------------------------------------------------------------
- myclass.h
- #ifndef MYCLASS_H
- #define MYCLASS_H
- #include <QObject>
- #include <QThread>
- #include "worker.h"
- class myclass : public QObject
- {
- Q_OBJECT
- public:
- explicit myclass(QObject *parent = nullptr);
- void init();
- void stop();
- private:
- worker *mWorker;
- signals:
- public slots:
- };
- #endif // MYCLASS_H
- -------------------------------------------------------------------------------------------------------------------
- worker.cpp
- #include "worker.h"
- #include <QThread>
- #include <QDebug>
- worker::worker(QObject *parent) : QObject(parent),
- mStop(false)
- {
- }
- void worker::stop()
- {
- mStop = true;
- }
- void worker::loop()
- {
- mStop = false;
- int i=0;
- while(!mStop) {
- QThread::msleep(500);
- qDebug() << "i : " << i;
- i++;
- }
- emit finished();
- }
- -------------------------------------------------------------------------------------------------------------------
- worker.h
- #ifndef WORKER_H
- #define WORKER_H
- #include <QObject>
- class worker : public QObject
- {
- Q_OBJECT
- public:
- explicit worker(QObject *parent = nullptr);
- void stop();
- private:
- bool mStop;
- signals:
- void finished();
- public slots:
- void loop();
- };
- #endif // WORKER_H
Add Comment
Please, Sign In to add comment