Q学习第31天-QThread

 新建一个类:

 在Main头文件中定义全局变量和槽函数:

 实现如下:使用QThread将一个文本框的值切换我是单数/我是复数

复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QThread>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mythread=new CustomeThread(this);
    connect(mythread,&CustomeThread::threadTimeout,this,&MainWindow::on_MyThreadChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btn_Start_clicked()
{
    mythread->start();
    qDebug()<<"启动:"<<endl;
}

void MainWindow::on_btn_Stop_clicked()
{
   mythread->terminate();
   qDebug()<<"停止:"<<endl;
}
void MainWindow::on_MyThreadChanged()
{
    index++;
    if(index%2==0)
    {
         ui->textEdit->setText("我是偶数");
    }
    else {
        ui->textEdit->setText("我是单数");
    }

}
复制代码

 源码下载:https://files.cnblogs.com/files/zxtang/SerialPortPro.rar?t=1715011742&download=true

 

posted @   WellMandala  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2021-05-08 如何理解内网和外网
点击右上角即可分享
微信分享提示