zoukankan      html  css  js  c++  java
  • qt中使用C++thread

    win.h

    #ifndef WIN_H
    #define WIN_H
    
    #include <QWidget>
    #include <thread>
    #include <chrono>
    #include <QDebug>
    
    
    
    
    class Win : public QWidget
    {
        Q_OBJECT
    
    public:
        Win(QWidget *parent = nullptr);
        ~Win();
    private:
        void hello();  //线程要调用的函数
        std::thread* t;
        //注意:这个变量不要在构造函数中定义成局部变量,否则构造函数结束,线程对象就释放了
    
    };
    #endif // WIN_H

    win.cpp

    #include "win.h"
    
    Win::Win(QWidget *parent)
        : QWidget(parent)
    {
        this->resize(400,300);
         t=new std::thread(&Win::hello,this);  //创建线程并启动
         //t  线程名
         //参数1:线程要执行的函数地址
    
    }
    
    Win::~Win()
    {
    }
    
    void Win::hello()
    {
        for (int i = 0; i < 30; i++) {
                qDebug() << "子线程:" << i;
                std::this_thread::sleep_for(std::chrono::seconds(2));  //本线程休眠2秒
            }
    }

    工程下载地址:链接:https://pan.baidu.com/s/18CQTU_i8WcJfif1CoUQmRA 提取码:6666   

  • 相关阅读:
    C++——文件的读写
    我以我血荐轩辕——记徐家福教授的演讲
    文件命名
    面向对象
    关于函数
    php跨域发送请求原理以及同步异步问题
    关于iframe
    关于url
    cookie
    call和apply
  • 原文地址:https://www.cnblogs.com/liming19680104/p/13911822.html
Copyright © 2011-2022 走看看