Main programmmainwindow.h#include <QMainWindow>#include <ttest.h>using namespace std;namespace Ui {class ddMainWindow;}class ddMainWindow : public QMainWindow{ Q_OBJECTpublic: explicit ddMainWindow(QWidget *parent = 0); ~ddMainWindow();private slots: void ShowInfo(int , off_t);private: Ui::ddMainWindow *ui; Ttest *test; //point to thread};mainwindow.cpp#include "mainwindow.h"#include "ui_mainwindow.h"ddMainWindow::ddMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::ddMainWindow){ ui->setupUi(this); test = new Ttest(this); // выделяю память для второстепенного потока QObject::connect(test, SIGNAL(changeVal(int, off_t)), SLOT(ShowInfo(int, off_t)), Qt::QueuedConnection); // связываю сигнал второстепенного потока с слотом главного потока //connect(test, SIGNAL(changeVal(int, off_t)), this, SLOT(ShowInfo(int, off_t))); // другой код связывания тоже пробовал// connect(test, SIGNAL(changeVal(int, off_t)), SLOT(ShowInfo(int, off_t))); // еще вариант связывания}ddMainWindow::~ddMainWindow(){ delete ui;}void ddMainWindow::ShowInfo(int progressVal, off_t time){ ui->progressBar->setValue(progressVal); // слот не вызывается по сигналу второстепенного потока changeVal(int, off_t)}void ddMainWindow::on_ddStartButton_clicked(){ test->start(); // запуск потока}----------------------------------------------------------------------------второстепенный потокttest.h#include <QThread>class Ttest : public QThread{ Q_OBJECTpublic: explicit Ttest(QObject *parent = 0); void run();signals: void changeVal(int, off_t);public slots:};----------------------------------------------ttest.cpp#include "ttest.h"Ttest::Ttest(QObject *parent) : QThread(parent){}void Ttest::run(){ int pv = 0; off_t t = 0; for (int x=0; x < 100; x ++) { emit changeVal(pv, t); // вызов сигнала для запуска слота передача значений слоту основного потока pv++; t++; sleep(1); }}
Main programmmainwindow.h#include <QMainWindow>#include <ttest.h>using namespace std;namespace Ui {class ddMainWindow;}class ddMainWindow : public QMainWindow{ Q_OBJECTpublic: explicit ddMainWindow(QWidget *parent = 0); ~ddMainWindow();private slots: void ShowInfo(int , off_t);private: Ui::ddMainWindow *ui; Ttest *test; //point to thread};Q_DECLARE_METATYPE(off_t)______________________________________________________________mainwindow.cpp#include "mainwindow.h"#include "ui_mainwindow.h"ddMainWindow::ddMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::ddMainWindow){ ui->setupUi(this); test = new Ttest(this); qRegisterMetaType<off_t>(); QObject::connect(test, SIGNAL(changeVal(int, off_t)), SLOT(ShowInfo(int, off_t)), Qt::QueuedConnection); }ddMainWindow::~ddMainWindow(){ delete ui;}void ddMainWindow::ShowInfo(int progressVal, off_t time){ ui->progressBar->setValue(progressVal);}void ddMainWindow::on_ddStartButton_clicked(){ test->start();}----------------------------------------------------------------------------potokttest.h#include <QThread>class Ttest : public QThread{ Q_OBJECTpublic: explicit Ttest(QObject *parent = 0); void run();signals: void changeVal(int, off_t);public slots:};----------------------------------------------ttest.cpp#include "ttest.h"Ttest::Ttest(QObject *parent) : QThread(parent){}void Ttest::run(){ int pv = 0; off_t t = 0; for (int x=0; x < 100; x ++) { emit changeVal(pv, t); pv++; t++; sleep(1); }}
main.cpp#include "ddmainwindow.h"#include <QApplication>bool StopIndicator;int main(int argc, char *argv[]){ QApplication a(argc, argv); StopIndicator = false; ddMainWindow w; w.show(); return a.exec();}
ddmainwindow.dd<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>ddMainWindow</class> <widget class="QMainWindow" name="ddMainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>528</width> <height>414</height> </rect> </property> <property name="windowTitle"> <string>ddMainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QTableWidget" name="tableWidget"> <property name="geometry"> <rect> <x>10</x> <y>180</y> <width>411</width> <height>171</height> </rect> </property> <property name="rowCount"> <number>3</number> </property> <property name="columnCount"> <number>3</number> </property> <row/> <row/> <row/> <column/> <column/> <column/> </widget> <widget class="QPushButton" name="ddStartButton"> <property name="geometry"> <rect> <x>10</x> <y>70</y> <width>83</width> <height>24</height> </rect> </property> <property name="text"> <string>start </string> </property> </widget> <widget class="QPushButton" name="fillTable"> <property name="geometry"> <rect> <x>430</x> <y>180</y> <width>91</width> <height>24</height> </rect> </property> <property name="text"> <string>Update disks</string> </property> </widget> <widget class="QLineEdit" name="lineEditdest"> <property name="geometry"> <rect> <x>220</x> <y>40</y> <width>191</width> <height>22</height> </rect> </property> </widget> <widget class="QPushButton" name="destDialog"> <property name="geometry"> <rect> <x>410</x> <y>40</y> <width>41</width> <height>22</height> </rect> </property> <property name="text"> <string>D</string> </property> </widget> <widget class="QPushButton" name="ddStopButton"> <property name="geometry"> <rect> <x>100</x> <y>70</y> <width>81</width> <height>22</height> </rect> </property> <property name="text"> <string>Stop</string> </property> </widget> <widget class="QProgressBar" name="progressBar"> <property name="geometry"> <rect> <x>10</x> <y>120</y> <width>481</width> <height>23</height> </rect> </property> <property name="value"> <number>0</number> </property> </widget> <widget class="QComboBox" name="comboBoxSrc"> <property name="geometry"> <rect> <x>10</x> <y>40</y> <width>191</width> <height>22</height> </rect> </property> </widget> <widget class="QLabel" name="labelSrc"> <property name="geometry"> <rect> <x>60</x> <y>20</y> <width>91</width> <height>16</height> </rect> </property> <property name="text"> <string>Disk source</string> </property> </widget> <widget class="QLabel" name="labelDest"> <property name="geometry"> <rect> <x>240</x> <y>20</y> <width>121</width> <height>16</height> </rect> </property> <property name="text"> <string>File Destination</string> </property> </widget> <widget class="QPushButton" name="ExitBut"> <property name="geometry"> <rect> <x>470</x> <y>10</y> <width>51</width> <height>22</height> </rect> </property> <property name="text"> <string>Exit</string> </property> </widget> <widget class="QLabel" name="labelInfo"> <property name="geometry"> <rect> <x>10</x> <y>150</y> <width>481</width> <height>16</height> </rect> </property> <property name="text"> <string>TextLabel</string> </property> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>528</width> <height>19</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>destDialog</sender> <signal>clicked()</signal> <receiver>ddMainWindow</receiver> <slot>SaveDestFile()</slot> <hints> <hint type="sourcelabel"> <x>434</x> <y>87</y> </hint> <hint type="destinationlabel"> <x>414</x> <y>111</y> </hint> </hints> </connection> <connection> <sender>ExitBut</sender> <signal>clicked()</signal> <receiver>ddMainWindow</receiver> <slot>ExitApp()</slot> <hints> <hint type="sourcelabel"> <x>496</x> <y>59</y> </hint> <hint type="destinationlabel"> <x>498</x> <y>75</y> </hint> </hints> </connection> <connection> <sender>ddStopButton</sender> <signal>clicked()</signal> <receiver>ddMainWindow</receiver> <slot>ddStop()</slot> <hints> <hint type="sourcelabel"> <x>174</x> <y>114</y> </hint> <hint type="destinationlabel"> <x>199</x> <y>122</y> </hint> </hints> </connection> </connections> <slots> <signal>changeVal(int,off_t)</signal> <slot>slotFillTable()</slot> <slot>SaveDestFile()</slot> <slot>ExitApp()</slot> <slot>ddStop()</slot> <slot>slotChangeVal(int,off_t)</slot> </slots></ui>