我在Qt / C ++中开发了一个应用程序,它是一个类似的文件浏览器。目前,在删除某些文件或复制某些文件时,我有一个管理整个应用程序和副本的线程,另一个刚刚创建的线程用于显示带有“取消”按钮的进度条。这是非常基本的,但是当我浏览Android设备文件系统时,对删除副本的任何访问都会冻结UI,并且应用程序线程会完成访问。这不是问题,因为我不希望用户在复制或删除时使用UI。
我的问题主要是线程的实时问题。当我使用Qt创建器作为IDE和调试时,我没有任何问题删除工作,并且进度条也显示没有任何问题。当我刚从Qt Creator中使用该应用程序时,该应用程序在尝试显示对话框时经常崩溃。
我非常确定它与线程的链接。当使用调试器和Qt创建器时,整个应用程序会减慢跟踪,调试......
以下是我要求删除时TreeView.cpp的代码:
DialogProgressIndicator *DeleteProgress = new DialogProgressIndicator;
DeleteProgress->moveToThread(&ProgressThread);
connect(&ProgressThread, &QThread::finished, DeleteProgress, &QObject::deleteLater);
connect(this, &PulsTreeWidget::DisplayProgress, DeleteProgress, &DialogProgressIndicator::ShowDlg);
connect(this, &PulsTreeWidget::UpdateProgress, DeleteProgress, &DialogProgressIndicator::UpdateIndicator);
connect(this, &PulsTreeWidget::CloseProgress, DeleteProgress, &DialogProgressIndicator::CloseDlg);
connect(DeleteProgress,&DialogProgressIndicator::CancelAction, this, &PulsTreeWidget::CatchActionCancel);
ProgressThread.start();
DisplayProgress();
删除完成后,我按照以下方法关闭所有内容:
CloseProgress();
ProgressThread.quit();
disconnect(&ProgressThread, &QThread::finished, FileTransferProgress, &QObject::deleteLater);
disconnect(this, &PulsTreeWidget::DisplayProgress, FileTransferProgress, &DialogProgressIndicator::ShowDlg);
disconnect(this, &PulsTreeWidget::UpdateProgress, FileTransferProgress, &DialogProgressIndicator::UpdateIndicator);
disconnect(this, &PulsTreeWidget::CloseProgress, FileTransferProgress, &DialogProgressIndicator::CloseDlg);
disconnect(FileTransferProgress,&DialogProgressIndicator::CancelAction, this, &PulsTreeWidget::CatchActionCancel);
类PulsTreeWidget定义如下:
class PulsTreeWidget : public QTreeWidget
{
Q_OBJECT
QThread ProgressThread;
public:
PulsTreeWidget(PulsDeviceMngr& device, PulsMainUI& parent);
~PulsTreeWidget();
signals:
void DisplayProgress();
void CloseProgress();
void UpdateProgress(int);
进度条由班级
管理DialogProgressIndicator.cpp
#include <QApplication>
#include "dialogprogressindicator.h"
#include "ui_dialogprogressindicator.h"
DialogProgressIndicator::DialogProgressIndicator(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogProgressIndicator)
{
ui->setupUi(this);
ui->progressBar->setRange(0,100);
ui->progressBar->setValue(1);
connect(ui->Cancel, SIGNAL(clicked()), this, SLOT(onClickCancel()));
}
void DialogProgressIndicator::ShowDlg() {
ui->progressBar->show();
}
void DialogProgressIndicator::CloseDlg() {
ui->progressBar->close();
}
DialogProgressIndicator::~DialogProgressIndicator()
{
delete ui;
}
void DialogProgressIndicator::UpdateIndicator(int value) {
ui->progressBar->setValue(value);
QApplication::processEvents();
}
void DialogProgressIndicator::onClickCancel() {
emit CancelAction();
disconnect(ui->Cancel, SIGNAL(clicked()), this, SLOT(onClickCancel()));
}
我遵循了Qt指南,但在执行&#34; DisplayProgress&#34;
时仍然崩溃有什么想法吗?在我执行
的另一个帖子中,它真的崩溃了void DialogProgressIndicator::ShowDlg() {
ui->progressBar->show();
}
我添加了崩溃日志:
Application Specific Information:
abort() called
Thread 0:: Dispatch queue: com.apple.main-thread
0 com.apple.AppKit 0x00007fff89e6561f -[NSView(NSInternal) _allocAuxiliary:] + 833
1 com.apple.AppKit 0x00007fff89e67a59 -[NSView _commonAwake] + 36
2 com.apple.AppKit 0x00007fff89e6c841 -[NSView initWithFrame:] + 457
3 libqcocoa.dylib 0x0000000103c2078f 0x103c0c000 + 83855
4 libqcocoa.dylib 0x0000000103c20a1d 0x103c0c000 + 84509
5 libqcocoa.dylib 0x0000000103c18f36 0x103c0c000 + 53046
6 libqcocoa.dylib 0x0000000103c14b72 0x103c0c000 + 35698
7 QtGui 0x0000000100f77603 QWindow::create() + 51
8 QtWidgets 0x0000000101509e13 QWidgetPrivate::create_sys(unsigned long long, bool, bool) + 1107
9 QtWidgets 0x00000001014df86c QWidget::create(unsigned long long, bool, bool) + 444
10 QtWidgets 0x00000001014eeebd QWidget::setVisible(bool) + 237
11 QtWidgets 0x000000010169679d QDialog::setVisible(bool) + 205
12 com.yourcompany.puls_connect 0x0000000100022305 DialogProgressIndicator::ShowDlg() + 21
13 com.yourcompany.puls_connect 0x000000010001a51e void QtPrivate::FunctionPointer<void (DialogProgressIndicator::*)()>::call<void, void>(void (DialogProgressIndicator::*)(), DialogProgressIndicator*, void**) + 142
14 com.yourcompany.puls_connect 0x000000010001a3fa QtPrivate::QSlotObject<void (DialogProgressIndicator::*)(), void, void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*) + 202
15 QtCore 0x0000000100bfdda2 QMetaObject::activate(QObject*, int, int, void**) + 1874
16 com.yourcompany.puls_connect 0x00000001000242db PulsTreeWidget::DisplayProgress() + 43
答案 0 :(得分:3)
请记住,您只能在主线程中使用GUI类。用于并行任务 QFutureWatcher:
WaitDialog waitDlg(this);
connect(this, SIGNAL(progress(int)), &waitDlg, SLOT(setProgress(int)));
QFutureWatcher<void> watcher;
connect(&watcher, SIGNAL(finished()), &waitDlg, SLOT(close()));
connect(&watcher, SIGNAL(canceled()), &waitDlg, SLOT(close()));
QFuture<void> future = QtConcurrent::run([] () {/*do parallel task here*/});
watcher.setFuture(future);
waitDlg.exec();