标签:private 创建对象 xpl prefix name object this ret 创建
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);");
}
标签:private 创建对象 xpl prefix name object this ret 创建
原文地址:https://www.cnblogs.com/gzk1171848896/p/9556240.html