【オリジナル】QTプログラミングマルチスレッド

11632 ワード

QTがインストールされていることを確認してください.参考にしてください.
http://www.cnblogs.com/kavs/p/4608926.html  QTをインストールします.
新規threadsフォルダ格納項目:mkdir threads
sudo gedit main.cpp
次のコードを入力します.
#include <QApplication>



#include "threaddialog.h"



int main(int argc, char *argv[]) 

{ 

    QApplication app(argc, argv); 

    ThreadDialog dialog; 

    dialog.show(); 

    return app.exec(); 

}

終了ファイルを保存します.たんまつにゅうりょく
sudo gedit threa.cpp
コードを入力:
#include <QtCore> 

#include <iostream>



#include "thread.h"



Thread::Thread() 

{ 

    stopped = false; 

}



void Thread::setMessage(const QString &message) 

{ 

    messageStr = message; 

}



void Thread::run() 

{ 

    while (!stopped) 

        std::cerr << qPrintable(messageStr); 

    stopped = false; 

    std::cerr << std::endl; 

}



void Thread::stop() 

{ 

    stopped = true; 

}

終了ファイルを保存します.たんまつにゅうりょく
sudo gedit thread.h
次のコードを入力します.
#ifndef THREAD_H 

#define THREAD_H



#include <QThread>



class Thread : public QThread 

{ 

    Q_OBJECT



public: 

    Thread();



    void setMessage(const QString &message); 

    void stop();



protected: 

    void run();



private: 

    QString messageStr; 

    volatile bool stopped; 

};



#endif

終了ファイルを保存します.たんまつにゅうりょく
sudo  gedit  threaddialog.cpp
次のコードを入力します.
#include <QtGui>



#include "threaddialog.h"



ThreadDialog::ThreadDialog(QWidget *parent) 

    : QDialog(parent) 

{ 

    threadA.setMessage("A"); 

    threadB.setMessage("B");



    threadAButton = new QPushButton(tr("Start A")); 

    threadBButton = new QPushButton(tr("Start B")); 

    quitButton = new QPushButton(tr("Quit")); 

    quitButton->setDefault(true);



    connect(threadAButton, SIGNAL(clicked()), 

            this, SLOT(startOrStopThreadA())); 

    connect(threadBButton, SIGNAL(clicked()), 

            this, SLOT(startOrStopThreadB())); 

    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));



    QHBoxLayout *mainLayout = new QHBoxLayout; 

    mainLayout->addWidget(threadAButton); 

    mainLayout->addWidget(threadBButton); 

    mainLayout->addWidget(quitButton); 

    setLayout(mainLayout);



    setWindowTitle(tr("Threads")); 

}



void ThreadDialog::startOrStopThreadA() 

{ 

    if (threadA.isRunning()) { 

        threadA.stop(); 

        threadAButton->setText(tr("Start A")); 

    } else { 

        threadA.start(); 

        threadAButton->setText(tr("Stop A")); 

    } 

}



void ThreadDialog::startOrStopThreadB() 

{ 

    if (threadB.isRunning()) { 

        threadB.stop(); 

        threadBButton->setText(tr("Start B")); 

    } else { 

        threadB.start(); 

        threadBButton->setText(tr("Stop B")); 

    } 

}



void ThreadDialog::closeEvent(QCloseEvent *event) 

{ 

    threadA.stop(); 

    threadB.stop(); 

    threadA.wait(); 

    threadB.wait(); 

    event->accept(); 

}

終了ファイルを保存します.たんまつにゅうりょく
sudo  threaddialog.h
次のコードを入力します.
#ifndef THREADDIALOG_H 

#define THREADDIALOG_H



#include <QDialog>



#include "thread.h"



class QPushButton;



class ThreadDialog : public QDialog 

{ 

    Q_OBJECT



public: 

    ThreadDialog(QWidget *parent = 0);



protected: 

    void closeEvent(QCloseEvent *event);



private slots: 

    void startOrStopThreadA(); 

    void startOrStopThreadB();



private: 

    Thread threadA; 

    Thread threadB; 

    QPushButton *threadAButton; 

    QPushButton *threadBButton; 

    QPushButton *quitButton; 

};



#endif

次にコンパイルを開始します.
端末でプロジェクトのフォルダにアクセスするには、次の手順に従います.
image
qmakeのコンパイルを開始–project threas.proファイルの生成
入力qmake threads.pro生成Makefileファイル入力make
コンパイラ.コンパイル完了結果
image
./threadsと入力
image
 
over..