让我们说,我有一个名为parallelRun
的程序。这需要一份工作人员列表,每个工作人员都有getWorkAmount():int
,run()
方法,finished()
信号和cancel()
广告位:
void parallelRun( std::vector< Worker* > workers );
其实施应该:
QPogressDialog
:unsigned int totalWorkAmount = 0;
for( auto it = workers.begin(); it != workers.end(); ++it )
{
totalWorkAmount += ( **it ).getWorkAmount();
}
LoadUI ui( 0, totalWorkAmount, this );
与
class LoadUI : public QObject
{
Q_OBJECT
public:
LoadUI( int min, int max, QWidget* modalParent )
: totalProgres( 0 )
, progressDlg( "Working", "Abort", min, max, modalParent )
{
connect( &progressDlg, SIGNAL( canceled() ), this, SLOT( cancel() ) );
progressDlg.setWindowModality( Qt::WindowModal );
progressDlg.show();
}
bool wasCanceled() const
{
return progressDlg.wasCanceled();
}
public slots:
void progress( int amount )
{
totalProgres += amount;
progressDlg.setValue( totalProgres );
progressDlg.update();
QApplication::processEvents();
}
signals:
void canceled();
private slots:
void cancel()
{
emit canceled();
}
private:
int totalProgres;
QProgressDialog progressDlg;
}
std::vector< std::unique_ptr< QThread > > threads;
for( auto it = workers.begin(); it != workers.end(); ++it )
{
std::unique_ptr< QThread > thread( new QThread() );
Worker* const worker = *it;
worker->moveToThread( thread.get() );
QObject::connect( worker, SIGNAL( finished() ), thread.get(), SLOT( quit() ) );
QObject::connect( &ui, SIGNAL( canceled() ), worker, SLOT( cancel() ) );
QObject::connect( *it, SIGNAL( progressed( int ) ), &ui, SLOT( progress( int ) ) );
thread->start( priority );
threads.push_back( std::move( thread ) );
}
for( auto it = workers.begin(); it != workers.end(); ++it )
{
QMetaObject::invokeMethod( *it, "run", Qt::QueuedConnection );
}
load()
在用户点击UI按钮时运行。
如果我希望在所有工作人员完成之前阻止parallelRun
阻止,而不冻结QProgressDialog
,我该如何扩展此代码?
我尝试在parallelRun
例程的末尾添加以下代码:
QApplication::processEvents();
for( auto it = threads.begin(); it != threads.end(); ++it )
{
( **it ).wait();
}
这几行额外代码的影响是LoadUI::progress
从未输入,因为GUI线程睡着了因此它的事件循环未处理:在Qt中,信号通过将它们发布到线程的事件循环传递到插槽,与线程所属的对象相关联。这就是为什么工人的progressed
信号永远不会被传递的原因。
我认为,适当的解决方案是在工作人员发出QApplication::processEvents()
信号的任何时候在GUI线程中运行progressed
。另一方面,我猜这是不可能的,因为GUI线程是睡着了。
另一种可能性是使用活动等待的解决方案:
for( auto it = threads.begin(); it != threads.end(); ++it )
{
while( ( **it ).isRunning() )
{
QApplication::processEvents();
}
}
for( auto it = threads.begin(); it != threads.end(); ++it )
{
( **it ).wait();
}
这还需要在thread->start( priority );
之后添加以下代码行:
while( !thread->isRunning() );
我不认为这是一个很好的解决方案,但至少它是有效的。如何在没有主动等待的缺点的情况下完成这项工作?
提前致谢!
答案 0 :(得分:1)
而不是建立自己的。也许你正在寻找QThreadPool?
QThreadPool具有等待所有工作人员的功能。
答案 1 :(得分:1)
您可以使用线程'finished()
信号等待它们全部在主GUI循环中完成,而不是使用QApplication::processEvents
。进度对话框模式将确保只有该对话框窗口处于活动状态才会显式关闭。
class WorkerManager : public QObject {
Q_OBJECT
private:
// to be able to access the threads and ui, they are defined as a members
std::vector<std::unique_ptr<QThread> > threads;
LoadUI *ui;
int finishedThreadCount;
public:
WorkerManager()
: finishedThreadCount(0)
{
// Open the QProgressDialog
...
// Create and start the threads
...
// Connect the finished() signal of each thread
// to the slot onThreadFinished
for( auto it = threads.begin(); it != threads.end(); ++it ) {
QObject::connect(
it->get(), SIGNAL(finished()),
this, SLOT(onThreadFinished()) );
}
}
private slots:
void onThreadFinished() {
++finishedThreadCount;
if(finishedThreadCount == threads.size())
{
// clean up the threads if necessary
// close the dialog
// and eventually destroy the object this itself
}
}
};
或者您可以运行嵌套的QEventLoop
来等待线程同步完成,同时仍然保持GUI响应:
// Open the QProgressDialog
...
// Create and start the threads
...
// Create and run a local event loop,
// which will be interrupted each time a thread finishes
QEventLoop loop;
for( auto it = threads.begin(); it != threads.end(); ++it )
{
QObject::connect(
it->get(), SIGNAL(finished()),
&loop, SLOT(quit()) );
}
for(int i = 0, threadCount = threads.size(); i < threadCount; ++i)
loop.exec();
如果仅在完成工作后进度达到最大值,您可以使用progressDlg->exec()
代替QEventLoop
,{{1}}将阻止直到达到最大值或直到用户点击“取消” “按钮。