Unsolved Hover Event - 2 windows
-
Hi,
I have 2 windows: main Window and QLabel ( Qt::Tool | Qt::FramelessWindowHint ). The main Window is in green color in pictures and QLabel is in blue color in pictures. QLabel is on the center of main Window.
I would like to have a situation like in a picture:
- mouse cursor is outside the main Window - for example in desktop ( label must be hide )
- mouse cursor is inside the main Window - I move mouse course from desktop to main Window ( label must be visible )
- mouse cursor is inside label - I move mouse cursor from mainWindow to label ( label must be visible )
- mouse cursor is inside the main Window - I move mouse cursor from label to main Window ( label must be visible )
- mouse cursor is outside the main Window ( label must be hide ) - I move mouse cursor to desktop
I don't know what I should write in hoverEnter and hoverLeave methods in main Window. When I have mouse cursor inside mainWindow and I move it to label I go to hoverLeave() ( because I leave main Window ), but when I move mouse cursor from main Window to desktop I go to hoverLeave() too. I can't find out, if I go to desktop or I go to label.
QCursor::pos is bad solution
-
Hi,
Check what you have under the cursor when the leave event occurs and if it's nothing then hide the label.
-
@SGaist Thank you for answer. Could you tell me what is "what you have under the cursor"? Any method?
-
-
@SGaist But how? I see here 1 problem:
In this method ( widgetAt ) I need QPoint. I can do something like:
In hoverLeave method: auto wid = QApplication::widgetAt(QCursor::pos());
I don't see any other method to get QPoint - only from QCursor::pos(). Event->pos() give me QPoint(-1,-1).
So QCursor::pos(). But this is bad solution because when I do very fast move my mouse I get strange position.
-
Which hoverLeave method ?
What about QWidget::leaveEvent ?
-
@SGaist hoverLeave method is my method which is execute when there is QHoverEvent. I think in this example QWidget::leaveEvent and QHoverEvent are very simillar.
-
Then please show your code.
-
@SGaist I don't have it on thic pc, so I can only give you some pseudoCode:
bool MainWindow::event(QEvent * e) { switch(e->type()) { case QEvent::HoverEnter: hoverEnter(static_cast<QHoverEvent*>(e)); return true; break; case QEvent::HoverLeave: hoverLeave(static_cast<QHoverEvent*>(e)); return true; break; default: break; } return QWidget::event(e); }
void MainWindow::hoverEnter(QHoverEvent * event) { if( !label -> isVisible() ) { label->show(); } } void MainWindow::hoverLeave(QHoverEvent * event) { if( label -> isVisible ) { label->hide(); } }
EDIT:
I have while(true) here :) When I have cursor in label I have loop:
hide->show->hide->show->hide->... -
Why a while ? You will only manage to block the event loop.
-
@SGaist Could you give me some snippet - I don't do this before
-
A snippet of what ?
-
@SGaist Blocking event loop
-
You do not want to block the event loop. That will freeze your GUI.
-
@SGaist Now in functions hoverEnter and hoverLeave I do:
qInfo()<<"enter"; or qInfo()<<"leave";
When my mouse cursor is in place, where label is I see in QT words:
enter, leave, enter, leave, enter, leave, enter, leave, enter, leave, enter, leave, enter, leave, enter, leave, ...And I see that my label is hidden -> shown ->hidden -> shown ->hidden -> shown ->hidden -> shown ->hidden -> shown ->hidden -> shown ->...
How can I do what I want ( picture ) ?