Qt:实现子线程发送信号父线程切换图片
mainwindow.h中代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mythread.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
MyThread* thread;
int count;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mythread.h中代码
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QLabel>
class MyThread : public QThread
{
Q_OBJECT
public:
QLabel* label;
//覆盖QThread中的run()函数
void run()
{
sleep(5);
emit done();//发送自定义信号done
}
signals:
void done();//自己定义的信号
};
#endif // MYTHREAD_H
main.cpp(创建时自动生成)
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp中代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
thread = new MyThread;//创建对象
thread->label = ui->label;
count = 0;
connect(thread,SIGNAL(done()),this,SLOT(on_pushButton_clicked()));//信号捕获
thread->start();//子线程
}
MainWindow::~MainWindow()
{
delete ui;
delete thread;
}
void MainWindow::on_pushButton_clicked()
{
count++;//图片在label中显示
if(1 == count)
ui->label->setStyleSheet("image: url(:/new/prefix1/image/1.jpeg);");
if(2 == count)
ui->label->setStyleSheet("image: url(:/new/prefix1/image/2.jpeg);");
}
最新文章
- arcgis desktop按ctrl键后地图乱移的解决办法
- loj 1150(spfa预处理+二分+最大匹配)
- 【Maven实战】传递性依赖的问题
- ubuntuOS
- 5)Javascript设计模式:extends模式
- [IOT] 自制蓝牙工牌办公室定位系统 (一)—— 阿里物联网平台概览及打通端到云(硬核&#183;干货)
- 浏览器本地数据库 IndexedDB 基础详解
- 关于Apache做负载均衡
- Nginx 热部署最版本
- 移动端开发demo—移动端web相册(一)
- java selenium操作safari
- 那些年读过的书《Java并发编程的艺术》一、并发编程的挑战和并发机制的底层实现原理
- 【Selenium-WebDriver自学】Log4J的设置(十五)
- JMeter安装+配置+运行
- Codeforces Round #369 (Div. 2) D. Directed Roads 数学
- Using PHP as a Spring MVC View via Quercus(转)
- Android开发关闭虚拟按钮、底部导航条
- iOS与硬件通讯(socket,data拼接,发送指令,解析指令)
- linux内核设计与实现一书阅读整理 之第一二章整合
- JDBC模板对象是多例的
热门文章
- Linux对外提供服务 网络操作 端口操作 1.开启服务监听端口 2.设置防火墙,放行访问端口的包 iptables&;netfilter 四表五链和通堵策略
- java基础笔记(三)——main方法
- 基于Vue的省市区三级联动插件
- Codeforces 1111D(退背包、排列组合)
- 寒假作业第二组E题题解
- php:两个文件夹递归地比较,没有的文件自动复制过去
- D、Homework of PE 容斥原理
- 使用 xib 设置 button 等款等高
- 【Java】 hashcode()和System.identityHashCode()
- EF6 CodeFirst连接MySql 报nvarchar(&#39;max&#39;)错误解决办法