Qt/Widgets/ProgressThreaded
From ProgrammingExamples
ProgressBarThreaded.cpp
#include <qapplication.h> #include "form.h" int main( int argc, char **argv ) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); }
form.cpp
#include "form.h" #include <iostream> #include <cmath> void MainWindow::StartProgressSlot() { std::cout << "StartProgressSlot()" << std::endl; this->progressBar->show(); } void MainWindow::StopProgressSlot() { std::cout << "StopProgressSlot()" << std::endl; this->progressBar->hide(); } void ProgressThread::run() { std::cout << "run()" << std::endl; emit StartProgressSignal(); for(unsigned int i = 0; i < 1e8; i++) { float a = sin(i); } std::cout << "Done." << std::endl; this->exit(); emit StopProgressSignal(); } void ProgressThread::exit() { std::cout << "exit()" << std::endl; emit StopProgressSignal(); } MainWindow::MainWindow() { this->setupUi(this); this->progressBar->setMinimum(0); this->progressBar->setMaximum(0); this->progressBar->hide(); connect(&myProgressThread, SIGNAL(StartProgressSignal()), this, SLOT(StartProgressSlot()), Qt::QueuedConnection); connect(&myProgressThread, SIGNAL(StopProgressSignal()), this, SLOT(StopProgressSlot()), Qt::QueuedConnection); } void MainWindow::on_btnStart_clicked() { std::cout << "Clicked start." << std::endl; this->myProgressThread.start(); // calls run() } void MainWindow::on_btnStop_clicked() { std::cout << "Clicked stop." << std::endl; this->myProgressThread.exit(); // calls exit() }
form.h
#ifndef BUTTONFORM_H #define BUTTONFORM_H #include "ui_main.h" #include <QThread> class ProgressThread : public QThread { Q_OBJECT public: void run(); void exit(); signals: void StartProgressSignal(); void StopProgressSignal(); }; class MainWindow : public QMainWindow, private Ui::MainWindow { Q_OBJECT public: MainWindow(); ProgressThread myProgressThread; public slots: void on_btnStart_clicked(); void on_btnStop_clicked(); void StartProgressSlot(); void StopProgressSlot(); }; #endif
main.ui
<?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>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QProgressBar" name="progressBar"> <property name="geometry"> <rect> <x>190</x> <y>220</y> <width>118</width> <height>23</height> </rect> </property> <property name="value"> <number>24</number> </property> </widget> <widget class="QPushButton" name="btnStart"> <property name="geometry"> <rect> <x>170</x> <y>340</y> <width>91</width> <height>28</height> </rect> </property> <property name="text"> <string>Start</string> </property> </widget> <widget class="QPushButton" name="btnStop"> <property name="geometry"> <rect> <x>360</x> <y>340</y> <width>91</width> <height>28</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>800</width> <height>25</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(ProgressBarThreaded) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs main.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(ProgressBarThreaded ProgressBarThreaded.cpp form.h form.cpp ${UISrcs} ${MOCSrcs}) TARGET_LINK_LIBRARIES(ProgressBarThreaded ${QT_LIBRARIES})