QLabel
当用户保存他们的游戏时,我试图在其中一个上创建一个褪色效果。
所以,我认为使用 aQThread
非常适合这项工作,但唯一的问题是QThread::msleep();
冻结程序。
我以前多次使用这种技术来减慢循环速度,但现在它只是决定冻结我的程序,直到循环结束。
有人对这里发生的事情有任何想法吗?
我删除了所有不相关的东西,因为这个程序很大。
保存.h
#ifndef SAVED_H
#define SAVED_H
#include <QThread>
class Saved : public QThread
{
Q_OBJECT
public:
explicit Saved(QObject *parent = 0);
void run();
signals:
void reduceVisibility(unsigned short);
public slots:
};
#endif // SAVED_H
保存的.cpp
#include "saved.h"
Saved::Saved(QObject *parent) :
QThread(parent)
{
}
void Saved::run(){
unsigned short alpha = 250;
for(unsigned short i = 0; i <= 5; i++){
emit reduceVisibility(alpha);
alpha -= 50;
QThread::msleep(250);
}
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
// QThread
#include "saved.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Saved *saving;
private slots:
public slots:
void animateSave(unsigned short);
private:
Ui::MainWindow *ui;
};
主文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// QThread
saving = new Saved(this);
connect(saving, SIGNAL(reduceVisibility(unsigned short)), this, SLOT(animateSave(unsigned short)));
}
MainWindow::~MainWindow()
{
delete ui;
}
// Buttons
void MainWindow::on_Btn_Save_clicked(){
QFile saveFile("save.txt");
saveFile.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&saveFile);
out << user.getUsername() + "\n" + user.getLevel();
saveFile.close();
saving->run();
saving->quit();
}
// Slots
void MainWindow::animateSave(unsigned short Visibility){
QString visible = QString::number(Visibility);
ui->Lbl_Saved->setStyleSheet("color:rgb(0,255,255, " + visible + ");");
}