Add a status dialog to the Qt project

advertisements

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?

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.