gpt4 book ai didi

c++ - QProgressdialog : busy waiting bar is not moving while function is executing

转载 作者:行者123 更新时间:2023-11-28 07:53:30 27 4
gpt4 key购买 nike

我试图在执行一个函数时显示一个繁忙的等待栏,我的问题是一旦函数启动它就停止移动。

MyProgressDialog *progBar= new MyProgressDialog();
QProgressBar* bar = new QProgressBar(progBar);
bar->setRange(0, 0);
bar->setValue(0);

progBar->setBar(bar);
QString labeltext=QString("<qt> <center><big><b>%1</b></big></center> <br><b>%2</b><br> %3 <br><b>%4</b><br> %5</qt>")
.arg(progBar->labeltext)
.arg("File in :")
.arg(FileI)
.arg("File out :")
.arg(FileO);
progBar->label->setText(labeltext);
progBar->setValue(10);
progBar->show();
progBar->setValue(20);
Sleep(500);
progBar->setValue(50);
Sleep(500);
MyFunction(FileI,FileO,mode,key);
Sleep(500);
progBar->setValue(80);
Sleep(500);
progBar->setValue(100);
progBar->close();
delete bar;
delete progBar;

我用 sleep 扭曲我的函数并设置值,目的是让它移动但徒劳无功,当我删除它们时,MyProgressdialog 没有显示其内容,我是否需要在单独的线程中使用我的函数? 我尝试使用 QFutureWatcher:

 QFutureWatcher<void> futureWatcher;
QFuture<void> f1 = run(
MyFunction,
filePath,
file.absolutePath()+"/OUT_"+fileN,
1,
key
);
QObject::connect(&futureWatcher, SIGNAL(finished()), progBar, SLOT(reset()));
QObject::connect(progBar, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int,int)), progBar, SLOT(setRange(int,int)));
QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), progBar, SLOT(setValue(int)));

// Start the computation.
futureWatcher.setFuture(f1);
// Display the dialog and start the event loop.
progBar->exec();
futureWatcher.waitForFinished();
delete progBar;

当我只调用 MyFunction 一次(针对一个文件)时,它工作正常并且我的条在移动,但是当我多次调用 MyFunction 时(连续针对许多文件)出现问题:它工作但我得到相同的 OUT_file对于所有处理过的文件,我认为这是多线程问题。

编辑: class MyProgressDialog

class MyProgressDialog: public QProgressDialog
{
Q_OBJECT

public:

MyProgressDialog()
{

qDebug()<<"MyProgressDialog constructor";

label=new QLabel(this);

QPalette* palette = new QPalette();
palette->setColor(QPalette::Window,"#F8F8FF");
setPalette(*palette);

QFont* font = new QFont("Courier New");
font->setItalic(true);
font->setPixelSize(15);
setFont(*font);
adjustSize();

setWindowIcon(QIcon(QApplication::applicationDirPath()+"/icons/icon1.png"));
setWindowFlags(Qt::WindowStaysOnTopHint);
setMinimumWidth(500);
setMinimumHeight(200);
labeltext=QString("Please wait until Encryption/Decryption was done");
label->setText(labeltext);
label->adjustSize();
label->setWordWrap (true);
setLabel(label);

setRange(0,100);
setWindowTitle("MyFunction progress");
setModal(true);

}
~MyProgressDialog()
{
qDebug()<<"MyProgressDialog destructor";
delete label;
}



public:

int value;
QString labeltext;
QLabel* label;

};

最佳答案

Qt 中的 UI 是事件驱动的。因此,在与 ui 相同的线程中执行您的代码将阻止每个 ui 事件,直到您的函数完成。有两种显示进度的方法。

  1. 使用线程进行计算并将更新事件发送给UI
  2. 更简单的方法:在每次 setValueshow 调用之后调用 QApplication::processEvents(); 静态方法。调用 QApplication::processEvents() 将调度当前在事件循环中排队的事件。这些事件包括所有 ui 相关事件

这是 gcc/mingw gcc 的代码示例

#include <QApplication>
#include <QProgressBar>

int main(int argc, char * argv[])
{
QApplication app(argc, argv);

QProgressBar bar;
bar.setRange(0, 100);
bar.show();
app.processEvents();
usleep(250000);

for(int i = 1; i <= 10; ++i)
{
bar.setValue(i * 10);
app.processEvents();
usleep(250000);
}

return 0;
}

它显示进度条并每 0.25 秒步进 10

您的代码应如下所示:

MyProgressDialog *progBar= new MyProgressDialog();
QProgressBar* bar = new QProgressBar(progBar);
bar->setRange(0, 100); // note your "busy state won't be shown as you're changing value right after show
bar->setValue(0);

progBar->setBar(bar);
QString labeltext=QString("<qt> <center><big><b>%1</b></big></center> <br><b>%2</b><br> %3 <br><b>%4</b><br> %5</qt>")
.arg(progBar->labeltext)
.arg("File in :")
.arg(FileI)
.arg("File out :")
.arg(FileO);
progBar->label->setText(labeltext);
progBar->setValue(10);
progBar->show();
QApplication::processEvents(); // HERE
progBar->setValue(20);
QApplication::processEvents(); // HERE
Sleep(500);
progBar->setValue(50);
QApplication::processEvents(); // HERE
Sleep(500);
MyFunction(FileI,FileO,mode,key);
Sleep(500);
progBar->setValue(80);
QApplication::processEvents(); // HERE
Sleep(500);
progBar->setValue(100);
progBar->close();
QApplication::processEvents(); // HERE
delete bar;
delete progBar;

关于c++ - QProgressdialog : busy waiting bar is not moving while function is executing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13229308/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com