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");
        }
    }
  • 相关阅读:
    Selenium2Library+ride学习笔记
    windbg 调试技巧
    LINUX常用命令--重定向、管道篇(四)
    Linux文件系统与结构
    windbg命令学习4
    windbg命令学习3
    windbg命令学习2
    MySQL常用操作命令
    Httpwatch 工具介绍
    windows平台上用python 远程线程注入,执行shellcode
  • 原文地址:https://www.cnblogs.com/defen/p/5330373.html
Copyright © 2011-2022 走看看