Hide/show the Cancel button in a QProgressDialog
I have a function that does series of actions, e.g. loading, processing and sending data, for several object instances in a loop. I use a QProgressDialog to display the current progress and interrupt it if necessary. However, during the data sending, due to particularities of the receiving side, it is extremely problematic to interrupt, so I would like to hide temporarily the Cancel button and to show it during the other steps.
The best that I did is the following:
for(i=0; i<N; i++) {
//loading
progress.setCancelButton(new QPushButton("Cancel"));
...
//processing
...
//sending
progress.setCancelButton(0);
...
}
The first loading is ok. However, after the button disappears during the first sending, it never appears there where it was. I don't have any idea how to place it anywhere, since I don't have any access to it?
Is there any solution? Thanks!
Re: Hide/show the Cancel button in a QProgressDialog
try reading this,
setCancelButton
Re: Hide/show the Cancel button in a QProgressDialog
Hmm... this is exactly what I used! setCancelButton(0) hides (it actually deletes) the button but the problem comes after that, when I create a new Cancel button. It appears at (0,0) in the dialog window, and I don't know how to move it there, where the previous one was. The code is functional, but it looks ugly :(
Re: Hide/show the Cancel button in a QProgressDialog
I would suggest to put the logic inside the slot QProgressDialog::cancel () such that the functionality of Cancel is ignored. Even if you press 'Cancel' button nothing will happen. Not intuitive though! :o
Re: Hide/show the Cancel button in a QProgressDialog
In that case it might be better to hold a QPointer<PushButton> instead of ignoring the action.
After looking into the qt source I think that the relayouting is missing. (in the qt source). You could check this if you trigger a resizeEvent for the Dialog.
Re: Hide/show the Cancel button in a QProgressDialog
Thanks guys!
I found a solution and here it is:
QList<QPushButton *> L=progressDialog.findChildren<QPushButton *>();
L.at(0)->hide();
L.at(0)->show();
Best regards!
Re: Hide/show the Cancel button in a QProgressDialog
If QProgressDialog and the 'Cancel' button shares the parent-child relationship you can use findChild to get the QObject and do QObject casting to get the pushbutton*. Then you can use QWidget::geometry() to get the co-ordinates. Save it and use the same for your logic.
Re: Hide/show the Cancel button in a QProgressDialog
I'd suggest disabling the button rather than hiding it. Users are normally happiest when the UI changes as little as possible, and keeping the button displayed but grayed out doesn't force a re-layout to occur.
Re: Hide/show the Cancel button in a QProgressDialog
Re: Hide/show the Cancel button in a QProgressDialog
Furthermore if you use setCancelButton() to set a new button, you don't need qFindChild to find it later. Just do it once before you enter the loop.