Add status dialog to Qt project

旧街凉风 提交于 2019-12-13 05:22:36

问题


I am making open source file manager with the ability to encrypt and decrypt file/files according given password called Cryptofm. You can get the code from here - the first version. I want to add status dialog, representing loading screen with progress bar for Dialog::encAll() slot, after the progress bar reached max value to close the statusdialog. I found out that I must firstly recursively find the total size of all files in the folder(in TreeView context menu option Size) - slot Dialog::dirSize() is doing this with the help of the function Dialog::getSelectedTreeItemSize(), and then set the progress bar property maximum to that value. Total size calculation proccess may take again a lot of time so I need another dialog with something moving to just indicate the process is executing. The whole thing should be something like the process of pasting very large folder with a lot of files in Windows 7.

Process of getting the total size:

Process of pasteing untill progress bar reach the total size:

The problem is that almost all functions,actions and etc are implemented in the Dialog class and I am not able to use threads - after adding QThread like this Dialog : public QDialog, public QThread in dialog.h(to be able to implement run() method) the program gives some errors:

C:\Users\niki\Documents\EncryptionProject\dialog.cpp:41: error: C2594: 'argument' : ambiguous conversions from 'Dialog *const ' to 'QObject *'

C:\Users\niki\Documents\EncryptionProject\dialog.cpp:46: error: C2594: 'argument' : ambiguous conversions from 'Dialog *const ' to 'QObject *'

C:\Users\niki\Documents\EncryptionProject\dialog.cpp:51: error: C2385: ambiguous access of 'connect' could be the 'connect' in base 'QObject' or could be the 'connect' in base 'QObject'

And another 31 errors, so:

  • What are the best options here?
  • Should I use MVC or another pattern?
  • Should I use threads?

回答1:


I don't get your entire question, but I can given you some hints.

The error "ambiguous conversion" tells you that C++ is unable to convert Dialog* const to QObject*. Normally you can solve this issue by using a cast like QObject* o = (QObject*) dialog. You also try to convert a pointer to a const object to a pointer to a non-const object. This is not possible as const objects are protected from changes whereas non-const objects are not. Try to remove the const qualifier or add it to the QObject*.

The behavior of the progress bar in your first screenshot is often called "indeterminate mode". You can achive this behavior with QProgressBar by setting the minimum and maxmimum values to 0 (use pbar->setMaximum(0) and pbar->setMinimum(0)).

As for your question about threads: Yes, you should use a worker thread to copy the files. The problem with using the UI-thread (which you probably use in your current solution) is, that the UI will stop respoding to user input (like moving the window or pressing the button) and you UI elements like the QProgressBar might not be updated and your progress therefor is not visible to the user. You added the QThread in a wrong manner to yout program. You currently inherited your custom Dialog class from QDialog (thats fine so far) and from QThread (thats the issue). Instead of inhereting from QThread, you should create a new QThread instance with new and then call a method with it. You'll find plenty of examples online.

You can use MVC, but it would bring you only a little benefit in your current situation. You also do not have model in the classical interpretation, although you could create a model handling the file operations.




回答2:


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.



来源:https://stackoverflow.com/questions/33730881/add-status-dialog-to-qt-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!