Code in 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
Code in Mythread.h
#ifndef Mythread_h
#define Mythread_h
#include <QThread>
#include <QLabel>
Class Mythread:public Qthread
{
Q_object
Public
qlabel* label;
Overwrite the run () function in Qthread
void Run ()
{
Sleep (5);
Emit done ();//Send custom signal done
}
Signals:
void done ();//self-defined signal
};
#endif//Mythread_h
Main.cpp (generated automatically when created)
#include <QtGui/QApplication>
#include "Mainwindow.h"
int main (int argc, char *argv[])
{
Qapplication A (argc, argv);
MainWindow W;
W.show ();
return A.exec ();
}
Code in 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;//Create object
Thread->label = ui->label;
Count = 0;
Connect (thread,signal (done)), This,slot (on_pushbutton_clicked ()));//Signal capture
Thread->start ();//Sub-thread
}
Mainwindow::~mainwindow ()
{
Delete UI;
Delete thread;
}
void Mainwindow::on_pushbutton_clicked ()
{
The count++;//picture is displayed in the 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);");
}
Qt: Implement child thread send signal parent thread toggle Picture