PDA

View Full Version : How to detect when a QDialog is clicked anywhere?



MarcCals
2nd March 2011, 22:19
Hello,

I would like to know if there is a way to dectect when user clicks anywhere in a QDialog.

We are using a QDialog to show a popup to the user, we would like that if user clicks the popup anywhere this hides. To detect if the user clicks the QDialog anywhere we have try it reimplementing mousePressEvent, but the problem is if the user click in a GroupBox that it's inside the QDialog or an Image, then we don't receive the mousePressEvent. It seems that the mousePressEvent only is thrown if the user clicks the region of QDialog with no widgets.

Thanks

wysota
3rd March 2011, 00:23
You mean you want a QSplashScreen ?

MarcCals
3rd March 2011, 07:58
No, We don't want a QSplashScreen, what we need is a PopUp to notify to user that request to retrieve some files has been received, We have implemented it with a QDialog. Ten seconds after appearing it hides automatically, but we would like that the user can click Popup anywhere to hide it if is disturbing, before the 10 seconds.

Lykurg
3rd March 2011, 08:44
What's about using QWidget::mousePressEvent() and close the widget there?

MarcCals
3rd March 2011, 09:44
We have tried QWidget::mousePressEvent(), but It not works. This PopUp contains inside others QWidgets like QLabel, QGroupBox,... If the users click the PopUp in a area with no others QWidgets mousePressEvent works, but if the user click for example on QGroupBox widget then the mousePressEvent is not thrown.

stampede
3rd March 2011, 09:55
Try to install event filter (http://doc.qt.nokia.com/latest/qobject.html#eventFilter) on every widget in the dialog:


MyDialog * dialog = new MyDialog();
// for each element in the window:
label->installEventFilter(dialog);
groupBox->installEventFilter(dialog);
// ...

bool MyDialog::eventFilter(QObject *obj, QEvent *event){
if (event->type() == QEvent::MouseButtonPress) {
this->close();
return true;
} else {
return false;
}
}
This code is not tested by me, I'm just wondering.

wysota
3rd March 2011, 09:58
No, We don't want a QSplashScreen, what we need is a PopUp to notify to user that request to retrieve some files has been received, We have implemented it with a QDialog. Ten seconds after appearing it hides automatically, but we would like that the user can click Popup anywhere to hide it if is disturbing, before the 10 seconds.

If you want a popup, then why don't you use Qt::Popup window flag with a regular widget?

MarekR22
3rd March 2011, 10:48
I would try QWidget::grabMouse.

MarcCals
3rd March 2011, 21:24
Using QWidget::grabMouse, It doesnt works because then no other widget except the PopUp receive mouseEvent. If you have another dialogs o widgets showed, they not receive mouseEvents.

Finally I have implemented it installing even widget to all widget ins the PopUp.

Thanks to all for your help,