QThread :: msleep()冻结程序

时间:2013-10-22 05:02:15

标签: c++ multithreading qt qthread qtcore

当用户保存游戏时,我正在尝试为我的QLabel之一创建一个淡化效果。

所以,我认为使用QThread对于这项工作来说是完美的,但唯一的问题是QThread::msleep();会冻结程序。

之前我已经多次使用过这种技术来减慢循环,但现在它决定冻结我的程序,直到循环结束。

有没有人对这里发生的事情有任何想法?

我删除了所有不相关的东西,因为这个程序非常庞大。

saved.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

saved.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);
    }
}

mainwindow.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;
};

的main.cpp

#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 + ");");
}

2 个答案:

答案 0 :(得分:3)

您还可以查找QPropertyAnimation的文档并将其挂钩在QWidget::windowOpacity上。这对你的问题来说可能是最合适的。

例如:

void MyWidget::buttonClicked() // SLOT when button is clicked
{
    QPropertyAnimation* anim = new QPropertyAnimation(ui->Lbl_Saved, "windowOpacity"); // new animation, hooked on the labels window opacity
    anim->setDuration(2500); // use 2.5 seconds for disapearing
    anim->setStartValue(1.0f); // from full visible
    anim->setEndValue(0.0f); // to invisible
    connect(anim, SIGNAL(finished()), anim, SLOT(deleteLater())); // delete object when animation done
    anim->start();
}

答案 1 :(得分:2)

  

有没有人对这里发生的事情有任何想法?

这是(see QThread: QObject)。接收者MainWindow在另一个线程中。由于默认连接为AutoConnection,因此Qt将使用QueuedConnection并在与animateSave对象相同的线程中运行广告位MainWindow

但是,您根本不应使用QThread。您不希望同时执行任何操作,而是希望在给定时间(或之后)发生某些事情QTimer就是你想要的。

MainWindow::MainWindow(QWidget *parent)
{
    /* ... */
    myTimer = new QTimer(this);
    connect(myTimer, SIGNAL(timeout(), this, SLOT(decreaseAlphaOrStop()));
}

void MainWindow::on_Btn_Save_clicked(){
    /* .. snip .. */
    alpha = 250;
    myTimer->start(250);    
}

// new slot
void MainWindow::decreaseAlphaOrStop(){
    alpha -= 50;
    if( alpha < 0){
        alpha = 0;
        myTimer->stop();
    }
    animateSave(alpha);
}