zoukankan      html  css  js  c++  java
  • qt中的多线程

    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::Dialog *ui;
    Mythread *myp;//此处声明了一个继承与pthread的类mythread,相当于创建了一个线程。
    };

    #endif // DIALOG_H

    2.dialog.cpp
    #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_pushButton_clicked() { myp = new Mythread(ui->label); myp->start();//自动执行mythread中的run()函数。相当于调用run函数 // QThread aa; // aa.start(); }
    3、mythread.h
    #ifndef MYTHREAD_H
    #define MYTHREAD_H #include <QThread> #include<QLabel> class Mythread : public QThread { public: Mythread(QLabel *l); protected: void run();//声明一个run函数,run为继承下来的一个虚函数 private: QLabel *pp; }; #endif // MYTHREAD_H
    #include"mythread.h"
    #include <QDebug>
    Mythread::Mythread(QLabel *l)//构造函数初始化
    {
       pp= l;
    
    
    }
    void Mythread::run()
    {
    
        while(1)
       {
        qDebug()<<"test";
        sleep(1);
        pp->setText("22");
         sleep(1);
        pp->setText("33");
        }
    }
  • 相关阅读:
    LeetCode 260
    LeetCode 258
    LeetCode 237
    LeetCode 226
    LeetCode 203
    LeetCode 202
    codeforces 7D
    codefroces 7C
    codeforces 7B
    codeforces 6E (非原创)
  • 原文地址:https://www.cnblogs.com/defen/p/5330373.html
Copyright © 2011-2022 走看看