I created a custom QWidget, that is NOT a qt::Popup, i overrided the MousePressEvent and put the same logic that the QWidget has in that method.
The source code of the QWidget hides the widget only if the click was made outside the widget's QRect.
And it is a fact that when i click on the button , the event is first catched on the MousePressEvent of the widget. If i hide the widget in that method, then the button recieves the click event. If i DONT hide the widget, then the button doesnt get the click event.
this is the overriden method:
{
//LoggerManager::LogDebug("[PrivateView::mousePressEvent] press event");
event->accept();
while ((w
= QApplication::activePopupWidget()) && w
!= this) {
w->close();
if (QApplication::activePopupWidget() == w
) // widget does not want to dissappear {
w->hide(); // hide at least
}
}
QPoint source
= mapToGlobal
(event
->pos
());
//check if the click is outside the widget and the button
if (source.x() >= this->x()
&& source.x() <= (this->x() + this->width())
&& source.y() >= (this->y() - btnHeight)
&& source.y() <= (this->y() + this->height()) )
{
// the click was made inside the widget or inside the button
}
else
{
hide();
}
}
void PrivateView::mousePressEvent(QMouseEvent *event)
{
//LoggerManager::LogDebug("[PrivateView::mousePressEvent] press event");
event->accept();
QWidget* w;
while ((w = QApplication::activePopupWidget()) && w != this)
{
w->close();
if (QApplication::activePopupWidget() == w) // widget does not want to dissappear
{
w->hide(); // hide at least
}
}
QPoint source = mapToGlobal(event->pos());
//check if the click is outside the widget and the button
if (source.x() >= this->x()
&& source.x() <= (this->x() + this->width())
&& source.y() >= (this->y() - btnHeight)
&& source.y() <= (this->y() + this->height()) )
{
// the click was made inside the widget or inside the button
}
else
{
hide();
}
}
To copy to clipboard, switch view to plain text mode
Added after 1 58 minutes:
So here's a slight modification. Here you'll see that im filtering the mouse press event, when the click was made inside or in the button, then we dont hide it and ignore the event. Otherwhise we hide the view and accept the event.
{
//LoggerManager::LogDebug("[PrivateView::mousePressEvent] press event");
while ((w
= QApplication::activePopupWidget()) && w
!= this) {
//LoggerManager::LogDebug("[PrivateView::mousePressEvent] hide: " + w->objectName());
w->close();
if (QApplication::activePopupWidget() == w
) // widget does not want to dissappear {
w->hide(); // hide at least
}
}
QPoint source
= mapToGlobal
(event
->pos
());
QPoint viewGlobalPoint
= this
->pos
();
//mapToGlobal(this->pos());
//check if the click is outside the widget and the button
if (source.x() >= viewGlobalPoint.x()
&& source.x() <= (viewGlobalPoint.x() + 200)
&& source.y() >= (viewGlobalPoint.y() - 25)
&& source.y() <= viewGlobalPoint.y())
{
// the click was made inside the button
event->ignore();
//hide(); --> if uncomment this, then the button recieves the click event
qDebug() << "click in the button area -> Do not hide, and ignore event";
}
else if (this->rect().contains(event->pos()))
{
// the click was made inside the widget
event->ignore();
qDebug() << "click inside view -> Do not hide, and ignore event";
}
else
{
event->accept();
hide();
emit clickOutside(source);
qDebug() << "hides the view, and accepts event";
}
}
void PrivateView::mousePressEvent(QMouseEvent *event)
{
//LoggerManager::LogDebug("[PrivateView::mousePressEvent] press event");
QWidget* w;
while ((w = QApplication::activePopupWidget()) && w != this)
{
//LoggerManager::LogDebug("[PrivateView::mousePressEvent] hide: " + w->objectName());
w->close();
if (QApplication::activePopupWidget() == w) // widget does not want to dissappear
{
w->hide(); // hide at least
}
}
QPoint source = mapToGlobal(event->pos());
QPoint viewGlobalPoint = this->pos();//mapToGlobal(this->pos());
//check if the click is outside the widget and the button
if (source.x() >= viewGlobalPoint.x()
&& source.x() <= (viewGlobalPoint.x() + 200)
&& source.y() >= (viewGlobalPoint.y() - 25)
&& source.y() <= viewGlobalPoint.y())
{
// the click was made inside the button
event->ignore();
//hide(); --> if uncomment this, then the button recieves the click event
qDebug() << "click in the button area -> Do not hide, and ignore event";
}
else if (this->rect().contains(event->pos()))
{
// the click was made inside the widget
event->ignore();
qDebug() << "click inside view -> Do not hide, and ignore event";
}
else
{
event->accept();
hide();
emit clickOutside(source);
qDebug() << "hides the view, and accepts event";
}
}
To copy to clipboard, switch view to plain text mode
In this case eventhough i ignore the event, the button doesnt recieve the click event. If i add the "hide()" in that block, then the button recieves the event.
Why is that happening? shouldnt the button recieve the event allways? why is the visible state of the widget interfering in the event procesing?
Thanks for your help!
Bookmarks