PDA

View Full Version : Hide/show the Cancel button in a QProgressDialog



Vankata
23rd February 2011, 03:59
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!

nikhilqt
23rd February 2011, 07:32
try reading this,

setCancelButton (http://doc.trolltech.com/main-snapshot/qprogressdialog.html#setCancelButton)

Vankata
23rd February 2011, 14:29
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 :(

nikhilqt
23rd February 2011, 15:00
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

nightghost
23rd February 2011, 16:16
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.

Vankata
23rd February 2011, 16:38
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!

nikhilqt
23rd February 2011, 16:46
If QProgressDialog and the 'Cancel' button shares the parent-child relationship you can use findChild (http://doc.qt.nokia.com/latest/qobject.html#findChild) to get the QObject and do QObject casting to get the pushbutton*. Then you can use QWidget::geometry() (http://doc.qt.nokia.com/latest/qwidget.html#geometry-prop) to get the co-ordinates. Save it and use the same for your logic.

SixDegrees
23rd February 2011, 16:47
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.

Vankata
24th February 2011, 17:21
Sure! You're right! :)

wysota
24th February 2011, 21:43
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.