Please ensure that QT is installed first, no reference
http://www.cnblogs.com/kavs/p/4608926.html install QT.
New Threads Folder Storage Project: MkDir threads
sudo gedit main.cpp
Enter the following code:
#include <QApplication>
#include "Threaddialog.h"
int main (int argc, char *argv[])
{
Qapplication app (argc, argv);
Threaddialog Dialog;
Dialog.show ();
return App.exec ();
}
Save the exit file.
Terminal input
sudo gedit threa.cpp
Type the code:
#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;
}
Save the exit file.
Terminal input
sudo gedit thread.h
Type the following code:
#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
Save the exit file.
Terminal input
sudo gedit threaddialog.cpp
Type the following code:
#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 ();
}
Save the exit file.
Terminal input
sudo threaddialog.h
Type the following code:
#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
Then start compiling:
Enter the project's folder in the terminal:
Start compiling qmake–project build Threas.pro file
Type qmake threads.pro generate makefile file, type make
Compile the program. Compile completed results
Type./threads
Over..
"Original" QT programming multithreading