Multi-Threading in QT

Source: Internet
Author: User

1, Dialog.h

#define Dialog_h

#include <QDialog>
#include "Mythread.h"
Namespace Ui {
Class Dialog;
}

Class Dialog:public Qdialog
{
Q_object

Public
Explicit Dialog (Qwidget *parent = 0);
~dialog ();

Private Slots:
void on_pushbutton_clicked ();

Private
Ui::D ialog *ui;
Mythread *myp;//here declares a class Mythread that inherits from Pthread, which is equivalent to creating a thread.
};

#endif//Dialog_h

2.dialog.cpp
#include"dialog.h"#include"ui_dialog.h"Dialog::D ialog (Qwidget*parent): Qdialog (parent), UI (NewUi::D ialog) {UI-&GT;SETUPUI ( This);} Dialog::~Dialog () {DeleteUI;}voiddialog::on_pushbutton_clicked () {MYP=NewMythread (ui->label); MYP-start ();//automatically executes the run () function in Mythread. Equivalent to calling the Run function//qthread AA; //Aa.start ();}
3, Mythread.h
#ifndef Mythread_h#defineMythread_h#include<QThread>#include<QLabel>classMythread: Publicqthread{ Public: Mythread (Qlabel*l); protected: voidrun ();//Declare a run function, run is a virtual function inheritedPrivate: Qlabel*pp;};#endif //Mythread_h
#include"mythread.h"#include<QDebug>Mythread::mythread (Qlabel*l)//constructor initialization {pp=l;}voidMythread::run () { while(1) {qdebug ()<<"Test"; Sleep (1); PP->settext (" A"); Sleep (1); PP->settext (" -"); }}

Multi-Threading in QT

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.