0

我正在制作能够根据给定密码 Cryptofm 加密和解密文件/文件的开源文件管理器。您可以从这里获取代码- 第一个版本。我想添加状态对话框,表示带有进度条的加载屏幕Dialog::encAll(),在进度条达到最大值后关闭状态对话框。我发现我必须首先递归地找到文件夹中所有文件的总大小(在 TreeView 上下文菜单选项大小中) - 插槽Dialog::dirSize()是在函数的帮助下做到这一点的Dialog::getSelectedTreeItemSize(),然后将进度条属性最大值设置为该值。总大小计算过程可能会再次花费大量时间,因此我需要另一个对话框,其中包含移动内容以指示该过程正在执行。整个过程应该类似于在 Windows 7 中粘贴包含大量文件的超大文件夹的过程。

获取总大小的过程:

在此处输入图像描述

粘贴直到进度条达到总大小的过程:

在此处输入图像描述

问题是几乎所有功能、动作等都在 Dialog 类中实现,我无法使用线程 -Dialog : public QDialog, public QThread在 dialog.h 中添加这样的 QThread 之后(能够实现 run() 方法)程序给出了一些错误:

C:\Users\niki\Documents\EncryptionProject\dialog.cpp:41:错误:C2594:'argument':从 'Dialog *const' 到 'QObject *' 的模糊转换

C:\Users\niki\Documents\EncryptionProject\dialog.cpp:46: error: C2594: 'argument' : 从 'Dialog *const' 到 'QObject *' 的模糊转换

C:\Users\niki\Documents\EncryptionProject\dialog.cpp:51:错误:C2385:“连接”的模糊访问可能是基础“QObject”中的“连接”,也可能是基础“QObject”中的“连接”

还有另外 31 个错误,所以:

  • 这里最好的选择是什么?
  • 我应该使用 MVC 还是其他模式?
  • 我应该使用线程吗?
4

2 回答 2

2

我不明白你的全部问题,但我可以给你一些提示。

错误“模糊转换”告诉您 C++ 无法转换Dialog* constQObject*. 通常你可以通过使用像QObject* o = (QObject*) dialog. 您还尝试将指向const对象的指针转换为指向非const对象的指针。这是不可能的,因为const对象受到保护而不会更改,而非const对象则不然。尝试删除const限定符或将其添加到QObject*.

第一个屏幕截图中进度条的行为通常称为“不确定模式”。pbar->setMaximum(0)您可以通过将最小值和最大值设置为 0(使用和pbar->setMinimum(0))来使用 QProgressBar 实现此行为。

至于您关于线程的问题:是的,您应该使用工作线程来复制文件。使用 UI 线程(您可能在当前解决方案中使用)的问题是,UI 将停止响应用户输入(例如移动窗口或按下按钮),并且您的 QProgressBar 等 UI 元素可能不会更新并且您的进度对用户不可见。您以错误的方式将 QThread 添加到您的程序中。您目前从 QDialog (到目前为止很好)和 QThread (这就是问题)继承了您的自定义 Dialog 类。您应该创建一个新的 QThread 实例,new然后用它调用一个方法,而不是从 QThread 继承。你会在网上找到很多例子。

您可以使用 MVC,但在您当前的情况下它只会给您带来一点好处。尽管您可以创建一个处理文件操作的模型,但您也没有经典解释中的模型。

于 2015-11-16T08:42:04.080 回答
0

I done something. It is not that easy like it looks. I have separated all the execution code in new class called threadedController and with moveToThread moved it in mainWindow to new thread. It is important to notice that this class is inheriting QObject in order to be able to use signal-slot mechanism, it have no parent in the constructor, becouse in other case it could not be moved to new thread. QWidget objects cannot be moved in new thread. It seems the comunication between GUI thread and new thread is able to be made by signal-slot mechanism. Qt is using Model/View architecture. Everyone can download the second version source and exe from here.

于 2015-11-25T16:52:53.863 回答