Selecting a label from a list of labels using a rubber band does not work properly
-
Hi there,
I have a number of labels, specifically 5, in a frame where I want to select one of those labels. In order to achieve this, I am using a rubber band. Next following a screenshot from the Qt Creator with the UI and the tree view of the UI
The frame I was talking about is the
videoStreamsFrame
and the labels are the ones inside this frame (videoStream1VideoFrame
,videoStream1VideoFrame
,videoStream2VideoFrame
,videoStream3VideoFrame
,videoStream4VideoFrame
andvideoStream5VideoFrame
).Next following the code that I have for the mouse event
// Global variables QRubberBand *rubberBand = nullptr; void MainWindow::mousePressEvent(QMouseEvent* event) { if (rubberBand) { delete rubberBand; rubberBand = nullptr; } QPoint mousePosition = event->globalPos(); // Iterate through all the QLabels from the Video Streams Frame to check if they contain the mouse pointer. // If a QLabel contains the mouse pointer, then this is the QLabel that was selected QList<QLabel*> qLabelList = ui->videoStreamsFrame->findChildren<QLabel*>(); foreach(QLabel *l, qLabelList) { QPoint mappedMousePosition = l->mapFromGlobal(mousePosition); if (l->geometry().contains(mappedMousePosition)) { rubberBand = new QRubberBand(QRubberBand::Rectangle, l); rubberBand->setGeometry(l->rect()); rubberBand->show(); break; } } } void MainWindow::mouseReleaseEvent(QMouseEvent* event) { if (rubberBand) { rubberBand->hide();// hide on mouse Release rubberBand->clearMask(); } }
With the above code, clicking on the
videoStream1VideoFrame
is working just fine. Clicking on thevideoStream2VideoFrame
, does not do anything. Clicking on thevideoStream3VideoFrame
, it's selecting thevideoStream2VideoFrame
. Clicking onvideoStream4VideoFrame
, does not do anything. Clicking on thevideoStream5VideoFrame
, it's selecting thevideoStream3VideoFrame
.I am new in Qt and I am not really sure what's the issue above. It might be a better way to achieve the above. I am open to suggestions.
Let me know if you need more information.
Kind regards,
Stavros
-
Hi all,
I finally managed to find out what was the problem. The fix is the following
@@ -26,7 +26,7 @@ void MainWindow::mousePressEvent(QMouseEvent* event) // If a QLabel contains the mouse pointer, then this is the QLabel that was selected QList<QLabel*> qLabelList = ui->videoStreamsFrame->findChildren<QLabel*>(); foreach(QLabel *l, qLabelList) { - QPoint mappedMousePosition = l->mapFromGlobal(mousePosition); + QPoint mappedMousePosition = l->mapToParent(l->mapFromGlobal(mousePosition)); if (l->geometry().contains(mappedMousePosition)) { rubberBand = new QRubberBand(QRubberBand::Rectangle, l); rubberBand->setGeometry(l->rect());
So, the mapFromGlobal() is translating the global coordinates to widget coordinates and the geometry(), where we use in order to check if the mouse coordinates are contained in the label, has the coordinates relative to the parent widget. So, we need to map the mouse coordinates to the parent widget, hence the function mapToParent().
Kind regards,
Stavros
-
Hi,
I don't understand what you're doing with your rubberband !
To find a child widget simply use :
QWidget *QWidget::childAt(int x, int y) const
Returns the visible child widget at the position (x, y) in the widget's coordinate system. If there is no visible child widget at the specified position, the function returns nullptr. -
Thanks for your reply.
I don't understand what you're doing with your rubberband !
What I am trying to do is, when I put the mouse pointer on one of the labels, to create a rubberband around it
To find a child widget simply use :
I am not sure how to use the child widget in my case. The issue that I have is that when I put the mouse pointer on some of the labels, it does not create a rubberband or it creates a rubberband in a different label. Is finding the child widget relevant to my issue? If yes, how can I use it?
Kind regards,
Stavros
-
Hi all,
I finally managed to find out what was the problem. The fix is the following
@@ -26,7 +26,7 @@ void MainWindow::mousePressEvent(QMouseEvent* event) // If a QLabel contains the mouse pointer, then this is the QLabel that was selected QList<QLabel*> qLabelList = ui->videoStreamsFrame->findChildren<QLabel*>(); foreach(QLabel *l, qLabelList) { - QPoint mappedMousePosition = l->mapFromGlobal(mousePosition); + QPoint mappedMousePosition = l->mapToParent(l->mapFromGlobal(mousePosition)); if (l->geometry().contains(mappedMousePosition)) { rubberBand = new QRubberBand(QRubberBand::Rectangle, l); rubberBand->setGeometry(l->rect());
So, the mapFromGlobal() is translating the global coordinates to widget coordinates and the geometry(), where we use in order to check if the mouse coordinates are contained in the label, has the coordinates relative to the parent widget. So, we need to map the mouse coordinates to the parent widget, hence the function mapToParent().
Kind regards,
Stavros