PDA

View Full Version : MessageLoop in DropDownColorPicker causes crash when parents get closed



j.mueller
8th February 2013, 18:01
Hello,

I'm trying to write me a color picker widget. The idea is, that it folds up from a smaller 'Select Color'-Button, and disappears if the user clickes anything else than the picker widget. So the widget is non-modal. (Think it similar to a drop down listbox.) Additional I would like to keep the applications UI responsive while the user is picking the color, to update the preview in the main view and - this is where the problem comes in - and to do this via an exec()-function containing a message loop (like the QFileDialog).

bool DropDownColorPickerWidget::exec()
{
m_result = true;
show();
QEventLoop eventLoop( this );
eventLoop.processEvents();

activateWindow();
while( isVisible() )
eventLoop.processEvents();

return m_result;
}
In general this seems to work, but when parent-widget of the picker widget is closed while the application is inside the pickers message loop this causes a crash.
So I´m looking for a possiblity for either preventing the parent widgets (and the application) from closing while the picker is poped up or for a way to exit the message loop before the deletion of the parent objects takes place.

Thanks in advance for any hints or advice.

Santosh Reddy
8th February 2013, 18:49
Try this (I have not tried this), but will be interested to know the results.
1. Implement/filter QWidget::closeEvent(), (of the parent window)
2. Hide the widget
3. Reject the event, i.e event->ignore()/return false;
4. deleteLater() the widget

anda_skoa
9th February 2013, 14:17
Nested event loops are usually a very bad idea and should only be used when really, really necessary and under full understanding what they do.

In your case you seem to not need it for any filtering, in fact you write that you want it to be have non-modal.
Why not just use the normal event processing and asynchronously return the result via signal?

Cheers,
_

j.mueller
11th February 2013, 09:18
Thanks a lot for your help.
I already tried the suggestions of Mr. Reddy. Although I also thought any of this approaches should do the job, nothing of it worked for me. But maybe I just implemented it in the right way.
So I think I´m now switching the widget to a more signal-slot based design.