Skip to content

Instantly share code, notes, and snippets.

@DeiVadder
Created May 31, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DeiVadder/1b9f5505920f52c33453a9ebe60e33e0 to your computer and use it in GitHub Desktop.
Save DeiVadder/1b9f5505920f52c33453a9ebe60e33e0 to your computer and use it in GitHub Desktop.
Qt ThreadExample
#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 "worker.h"
#include "QThread"
#include <QtConcurrent>
#define RAND_MAX = 20;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//!QtConcurrent
connect(ui->btnQtConcurrent, &QPushButton::clicked, this,&MainWindow::startConcurrent);
connect(this, &MainWindow::newConcurrentValue, this, &MainWindow::updateVonConcurrent);
//!QThread
//Initalisierung des Threads und der Workerklasse
QThread * workerThread = new QThread();
Worker *workerObject = new Worker();
workerObject->moveToThread(workerThread);
connect(workerThread, &QThread::started, workerObject, &Worker::init);
//QThread und Worker aufräumen bei Programmende
connect(workerThread, &QThread::finished, workerThread, &QThread::deleteLater);
connect(workerThread, &QThread::finished, workerObject, &Worker::deleteLater);
connect(qApp, &QApplication::aboutToQuit, workerThread, &QThread::quit);
// Interaktion mit der Benutzeroberfläche
connect(ui->btnQThread, &QPushButton::clicked, workerObject, &Worker::start);
connect(workerObject, &Worker::qThreadResult, ui->lblResult,&QLabel::setText);
connect(ui->dSpinBase, QOverload<double>::of(&QDoubleSpinBox::valueChanged), workerObject, &Worker::newBase);
connect(ui->dSpinExp, QOverload<double>::of(&QDoubleSpinBox::valueChanged), workerObject, &Worker::newExponent);
workerThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startConcurrent()
{
QtConcurrent::run([=]{
int i = 0;
while( i < 10001){
if (qrand() == 5){
emit newConcurrentValue(i);
i++;
}
}
});
}
void MainWindow::updateVonConcurrent(int value)
{
ui->progressBar->setValue(value);
ui->lblQtConcurrent->setText(QString::number(value) + "von 10000");
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void newConcurrentValue(int value);
private slots:
void startConcurrent();
void updateVonConcurrent(int value);
private:
Ui::MainWindow *ui;
};
#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>723</width>
<height>300</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
</font>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<property name="font">
<font>
<pointsize>15</pointsize>
</font>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btnQtConcurrent">
<property name="text">
<string>Start QtConcurrent</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="lblQtConcurrent">
<property name="text">
<string>0 von 10000</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="maximum">
<number>10000</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="btnQThread">
<property name="text">
<string>Start QThread</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDoubleSpinBox" name="dSpinBase">
<property name="value">
<double>10.199999999999999</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lblText">
<property name="text">
<string>hoch</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="dSpinExp">
<property name="value">
<double>2.500000000000000</double>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="lblResult">
<property name="text">
<string>Ergebnis</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>723</width>
<height>43</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/>
</ui>
#-------------------------------------------------
#
# Project created by QtCreator 2017-05-31T16:27:44
#
#-------------------------------------------------
QT += core gui concurrent
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ThreadExample
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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
SOURCES += \
main.cpp \
mainwindow.cpp \
worker.cpp
HEADERS += \
mainwindow.h \
worker.h
FORMS += \
mainwindow.ui
#include "worker.h"
#include "qmath.h"
Worker::Worker(QObject *parent) : QObject(parent)
{
}
void Worker::init()
{
timer = new QTimer(this);
timer->setInterval(100);
connect(timer, &QTimer::timeout, this, &Worker::calculate);
}
void Worker::start()
{
if(timer){
timer->start();
}
}
void Worker::newBase(double value)
{
m_Base = value;
}
void Worker::newExponent(double value)
{
m_Expo = value;
}
void Worker::calculate()
{
qreal result = qPow(m_Base, m_Expo);
emit qThreadResult(QString::number(result) + " um "+QTime::currentTime().toString("mm:hh:ss.zzz"));
}
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QTimer>
#include <QTime>
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = nullptr);
public slots:
void init();
void start();
public slots:
void newBase(double value);
void newExponent(double value);
signals:
void qThreadResult(QString str);
private slots:
void calculate();
private:
double m_Base = 10.5;
double m_Expo = 2.3;
QTimer *timer = Q_NULLPTR;
};
#endif // WORKER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment