"Original" QT programming multithreading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.