简体   繁体   English

QThread :: msleep()冻结程序

[英]QThread::msleep() freezes program

I'm trying to create a fading effect on one of my QLabel when the user saves their game. 当用户保存游戏时,我试图在我的QLabel上创建一种淡入淡出效果。

So, i thought using a QThread would be perfect for the job but the only problem is QThread::msleep(); 因此,我认为使用QThread非常适合这项工作,但唯一的问题是QThread::msleep(); freezes the program. 冻结程序。

I've used this technique many times before to slow down a loop but, now it just decided to freezes my program until the loop is over. 在此之前,我已经多次使用这种技术来减慢循环速度,但是现在,它决定冻结程序直到循环结束。

Does anyone have any ideas about what's going on here? 有人对这里发生的事情有任何想法吗?

I removed all the unrelated stuff cause this program is huge. 我删除了所有无关的东西,因为该程序很大。

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 保存.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 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 + ");");
}

You may also lookup the documentation of QPropertyAnimation and hook it on the QWidget::windowOpacity . 您也可以查找QPropertyAnimation的文档,并将其挂在QWidget::windowOpacity this is may be the most fitting thing for your problem. 这可能是最适合您的问题的东西。

for example: 例如:

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();
}

Does anyone have any ideas about what's going on here? 有人对这里发生的事情有任何想法吗?

This is intended (see QThread: QObject) . 这是有意的(请参阅QThread:QObject) The receiver MainWindow is in another thread. 接收器MainWindow在另一个线程中。 Since the default connection is AutoConnection , Qt will use a QueuedConnection and run the slot animateSave in the same thread as your MainWindow object. 由于默认连接为AutoConnection ,因此Qt将使用QueuedConnection并在与MainWindow对象相同的线程中运行animateSave插槽。

However, you shouldn't use QThread at all. 但是,您根本不应该使用QThread You don't want to do things at the same time, instead, you want something to happen at (or after) a given time . 您不想同时做某事,而是希望在给定时间(或之后)发生某事。 QTimer is what you want. 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM