最近在看qt,书中的线程例子,就自己复现了一下,效果如下,不同的点数对应不同的图片。
首先新建一个所需的项目
创建完成之后会有widget类和相关的ui
dialog是我自己创建的类,没有使用widget类,过程如下:
首先编写添加qdicethread.h头文件
代码如下
#ifndef QDICETHREAD_H
#define QDICETHREAD_H
#include <QObject>
#include <QThread>
class qdicethread : public QThread
{
Q_OBJECT
private:
int my_seq=0;
int my_dicevalue;
bool my_pause=true;
bool my_stop=false;
signals:
void newValue(int x,int y);
protected:
void run() Q_DECL_OVERRIDE;
public:
qdicethread();
void diceBegin();
void dicePause();
void stopThread();
};
#endif // QDICETHREAD_H
qdicethread.c中实现相关程序
#pragma comment (lib, "User32.lib")
#include "qdicethread.h"
#include <QTime>
#include <iostream>
qdicethread::qdicethread()
{
}
void qdicethread::diceBegin()
{
my_pause=false;
}
void qdicethread::dicePause()
{
my_pause=true;
}
void qdicethread::stopThread()
{
my_stop=true;
}
void qdicethread::run()
{
my_stop=false;
my_seq=0;
qsrand(QTime::currentTime().msec());
while(!my_stop)
{
if(!my_pause)
{
my_dicevalue=qrand();
my_dicevalue=(my_dicevalue%6+1);
my_seq++;
//std::cout<<my_seq<<" "<<my_dicevalue<<std::endl;
emit newValue(my_seq,my_dicevalue);
}
msleep(500);
}
quit();
}
然后编写qt的显示文件,槽和信号,以及显示界面
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include "qdicethread.h"
#include <QDialog>
#include <QCloseEvent>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
private:
qdicethread threadA;
protected:
void colseEvent (QCloseEvent *event);
public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
void onthreadA_started();
void onthreadA_finished();
void onthreadA_newValue(int seq,int diceValue);
void on_startprocess_clicked();
void on_finishedprocess_clicked();
void on_startdice_clicked();
void on_pausedice_clicked();
void on_pushButton_3_clicked();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp
#pragma comment (lib, "User32.lib")
#include "dialog.h"
#include "ui_dialog.h"
#include <QString>
#include <iostream>
#include <QLabel>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(&threadA,SIGNAL(started()),this,SLOT(onthreadA_started()));
connect(&threadA,SIGNAL(finished()),this,SLOT(onthreadA_finished()));
connect(&threadA,SIGNAL(newValue(int,int)),this,SLOT(onthreadA_newValue(int,int)));
}
void Dialog::onthreadA_started()
{
ui->lineEdit->setText("Thread status: thread started");
}
void Dialog::onthreadA_finished()
{
ui->lineEdit->setText("Thread status: thread finished");
}
void Dialog::onthreadA_newValue(int seq,int diceValue)
{
QString str=QString::asprintf("throw num:%d,point is:%d",seq,diceValue);
ui->textEdit->append(str);
// std::cout<<str.toStdString()<<std::endl;
QPixmap pic;
QString filename=QString::asprintf("D:/homework/qt/multi_process/dice/images/%d.jpg",diceValue);
pic.load(filename);
QPixmap *pixmap = new QPixmap("filename");
pixmap->scaled(ui->label->size(), Qt::KeepAspectRatio);
ui->label->setScaledContents(true);
ui->label->setPixmap(pic);
ui->label->show();
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_startprocess_clicked()
{
threadA.start();
ui->startprocess->setEnabled(false);
ui->finishedprocess->setEnabled(true);
ui->startdice->setEnabled(true);
ui->pausedice->setEnabled(false);
}
void Dialog::on_finishedprocess_clicked()
{
threadA.stopThread();
threadA.wait();
ui->startprocess->setEnabled(true);
ui->finishedprocess->setEnabled(false);
ui->startdice->setEnabled(false);
ui->pausedice->setEnabled(false);
}
void Dialog::on_startdice_clicked()
{
threadA.diceBegin();
std::cout<<"threadA.diceBegin();"<<std::endl;
ui->startdice->setEnabled(false);
ui->pausedice->setEnabled(true);
}
void Dialog::on_pausedice_clicked()
{
threadA.dicePause();
std::cout<<"threadA.dicePause();"<<std::endl;
ui->startdice->setEnabled(true);
ui->pausedice->setEnabled(false);
}
void Dialog::colseEvent(QCloseEvent *event)
{
if(threadA.isRunning())
{
threadA.stopThread();
threadA.wait();
}
event->accept();
}
void Dialog::on_pushButton_3_clicked()
{
ui->textEdit->clear();
std::cout<<"ui->textEdit->clear();"<<std::endl;
//恢复初始格式
QFont f;
f.setFamily("微软雅黑");
f.setPointSize(10);
ui->textEdit->selectAll();
ui->textEdit->setCurrentFont(f);
ui->textEdit->setTextColor(Qt::black);
ui->textEdit->setTextBackgroundColor(Qt::white);
}
下面是dialog.ui使用的控件
完成之久点击运行便可以实现了。
下面是遇到的一些问题
1.
一直出现这个问题,查文章有说让删除debug或者添加#pragma comment (lib, “User32.lib”),最后发现是自己没有在类中添加Q_OBJECT,添加之后执行qmake然后就可以正常运行了。
2.关于图片和显示
label就是所用的的Qlabel,调节图片自适应大小
3.
在main函数中关闭widget的界面显示。
qt新手有错误请大家指出,如果有帮助请点个赞。