Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Selecting a label from a list of labels using a rubber band does not work properly
QtWS25 Last Chance

Selecting a label from a list of labels using a rubber band does not work properly

Scheduled Pinned Locked Moved Solved General and Desktop
qrubberbandqlabelselection
4 Posts 2 Posters 654 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stavros Vaionitis
    wrote on last edited by Stavros Vaionitis
    #1

    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

    20ac9062-79a1-4027-8518-6f35d0e8d79a-image.png

    The frame I was talking about is the videoStreamsFrame and the labels are the ones inside this frame (videoStream1VideoFrame, videoStream1VideoFrame, videoStream2VideoFrame, videoStream3VideoFrame, videoStream4VideoFrame and videoStream5VideoFrame).

    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 the videoStream2VideoFrame, does not do anything. Clicking on the videoStream3VideoFrame, it's selecting the videoStream2VideoFrame. Clicking on videoStream4VideoFrame, does not do anything. Clicking on the videoStream5VideoFrame, it's selecting the videoStream3VideoFrame.

    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

    M 1 Reply Last reply
    0
    • S Offline
      S Offline
      Stavros Vaionitis
      wrote on last edited by
      #4

      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

      1 Reply Last reply
      0
      • S Stavros Vaionitis

        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

        20ac9062-79a1-4027-8518-6f35d0e8d79a-image.png

        The frame I was talking about is the videoStreamsFrame and the labels are the ones inside this frame (videoStream1VideoFrame, videoStream1VideoFrame, videoStream2VideoFrame, videoStream3VideoFrame, videoStream4VideoFrame and videoStream5VideoFrame).

        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 the videoStream2VideoFrame, does not do anything. Clicking on the videoStream3VideoFrame, it's selecting the videoStream2VideoFrame. Clicking on videoStream4VideoFrame, does not do anything. Clicking on the videoStream5VideoFrame, it's selecting the videoStream3VideoFrame.

        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

        M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #2

        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.

        S 1 Reply Last reply
        0
        • M mpergand

          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.

          S Offline
          S Offline
          Stavros Vaionitis
          wrote on last edited by
          #3

          @mpergand

          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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Stavros Vaionitis
            wrote on last edited by
            #4

            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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved