Skip to content

Instantly share code, notes, and snippets.

@aol-nnov
Created May 22, 2020 07:02
Show Gist options
  • Save aol-nnov/615770cbfbbd5d6834a1cec314fbb59b to your computer and use it in GitHub Desktop.
Save aol-nnov/615770cbfbbd5d6834a1cec314fbb59b to your computer and use it in GitHub Desktop.
#-------------------------------------------------
#
# Project created by QtCreator 2020-05-22T09:28:09
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = demo
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp \
worker.cpp
HEADERS += \
mainwindow.h \
worker.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget* parent):
QMainWindow(parent),
ui(new Ui::MainWindow),
workerThread(this),
worker(new Worker),
workerTimer(new QTimer(this)) {
ui->setupUi(this);
worker->moveToThread(&workerThread);
workerThread.start();
connect(workerTimer, &QTimer::timeout, worker, &Worker::makeResult);
}
MainWindow::~MainWindow() {
stopWorker();
workerThread.quit();
workerThread.wait();
delete worker;
delete ui;
}
void MainWindow::startWorker() {
workerTimer->start(10000);
qDebug() << "Worker timer started";
}
void MainWindow::stopWorker() {
workerTimer->stop();
qDebug() << "Worker timer stopped";
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "worker.h"
#include <QMainWindow>
#include <QThread>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow: public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget* parent = nullptr);
~MainWindow() override;
private slots:
void startWorker(void);
void stopWorker(void);
private:
Ui::MainWindow* ui;
QThread workerThread;
Worker* worker;
QTimer* workerTimer;
};
#endif // MAINWINDOW_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>88</width>
<height>34</height>
</rect>
</property>
<property name="text">
<string>start</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>180</x>
<y>20</y>
<width>88</width>
<height>34</height>
</rect>
</property>
<property name="text">
<string>stop</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>30</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>startWorker()</slot>
<hints>
<hint type="sourcelabel">
<x>98</x>
<y>80</y>
</hint>
<hint type="destinationlabel">
<x>128</x>
<y>154</y>
</hint>
</hints>
</connection>
<connection>
<sender>pushButton_2</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>stopWorker()</slot>
<hints>
<hint type="sourcelabel">
<x>232</x>
<y>81</y>
</hint>
<hint type="destinationlabel">
<x>270</x>
<y>159</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>startWorker()</slot>
<slot>stopWorker()</slot>
</slots>
</ui>
#include "worker.h"
#include <QDebug>
#include <unistd.h>
Worker::Worker(QObject* parent): QObject(parent) {
}
void Worker::makeResult() {
qDebug() << "starting long run" << taskNum;
sleep(3);
taskNum++;
qDebug() << "task completed";
emit resultReady(taskNum);
}
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
class Worker: public QObject {
Q_OBJECT
public:
explicit Worker(QObject* parent = nullptr);
bool isWorking();
signals:
void resultReady(int);
public slots:
void makeResult();
private:
bool isWorking_ { false };
int taskNum { 0 };
};
#endif // WORKER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment