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. Draw rectangle on QLabel
Qt 6.11 is out! See what's new in the release blog

Draw rectangle on QLabel

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 10.8k Views 2 Watching
  • 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by
    #6

    Hello All,
    @mrjj @jsulm @dheerendra :

    Thanks for your reply everyone.
    I tried to implement the above functionality using QRubberband.
    I tried to follow an approach where i tried to implement the rubberband functionality on the QLabel
    using EventFilter.

    My problem is that my mousepress event works fine. But when go to mouse move event my code crashes.

    Sample code is given below:

    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    if (event->type() == QEvent::MouseButtonPress)
    {
    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
    statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
    origin = mouseEvent->pos();
    if (!rubberBand)
    rubberBand = new QRubberBand(QRubberBand::Line, this);
    rubberBand->setGeometry(QRect(origin, QSize()));
    rubberBand->show();
    qDebug("Entered the Mousebutton press Event");
    }

    if (event->type() == QEvent::MouseMove)
    {
    qDebug("Entering the mouse move event");
    counter ++;
    qDebug()<<counter;
    QMouseEventmouseEvent = static_cast<QMouseEvent>(event);
    rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized()) // The code crashes at this line.
    qDebug("Completed the mouse move event");
    }
    return false;
    }

    Thanks in advance

    jsulmJ 1 Reply Last reply
    0
    • KiraK Kira

      Hello All,
      @mrjj @jsulm @dheerendra :

      Thanks for your reply everyone.
      I tried to implement the above functionality using QRubberband.
      I tried to follow an approach where i tried to implement the rubberband functionality on the QLabel
      using EventFilter.

      My problem is that my mousepress event works fine. But when go to mouse move event my code crashes.

      Sample code is given below:

      bool MainWindow::eventFilter(QObject *obj, QEvent *event)
      {
      if (event->type() == QEvent::MouseButtonPress)
      {
      QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
      statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
      origin = mouseEvent->pos();
      if (!rubberBand)
      rubberBand = new QRubberBand(QRubberBand::Line, this);
      rubberBand->setGeometry(QRect(origin, QSize()));
      rubberBand->show();
      qDebug("Entered the Mousebutton press Event");
      }

      if (event->type() == QEvent::MouseMove)
      {
      qDebug("Entering the mouse move event");
      counter ++;
      qDebug()<<counter;
      QMouseEventmouseEvent = static_cast<QMouseEvent>(event);
      rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized()) // The code crashes at this line.
      qDebug("Completed the mouse move event");
      }
      return false;
      }

      Thanks in advance

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #7

      @Kira Shouldn't it be

      QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
      rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized())) ;
      

      Was rubberBand initialized?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • KiraK Offline
        KiraK Offline
        Kira
        wrote on last edited by Kira
        #8

        @jsulm : thanks for the reply it worked like a charm.
        The issue got resolved now i can draw rubberband easily.

        The rubbandband get drawn easily when i assign QRubberband to main window widget.
        But when i try to assign it to QLabel there is issue with the drawing of rubberband.
        I search on the above topic and came to know it is because of difference of the co-ordinates of window and label and i have to map it globally using the below link:
        https://stackoverflow.com/questions/16527248/qrubberband-on-a-definite-label
        But the problem still exist and i am not able to draw the rectangle.
        //Example code:
        void MainWindow::mousePressEvent(QMouseEvent *event){

        mypoint = ui->label->mapFromGlobal(this->mapToGlobal(event->pos()));
        // mypoint = ui->label->mapFrom(this, event->pos());
        // mypoint = this->mapTo(ui->label, event->pos());
        if(rubberBand == 0) // You should add this to not have a memory leak
            rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label_2);//new rectangle band
        rubberBand->setGeometry(QRect(mypoint, ui->label_2->size()));
        rubberBand->show();
        

        }
        After following the above approach,
        When i click the mouse inside the label the whole rubberband gets drawn without even moving the cursor.

        Another approach would be making a custom QLabel and implement the mouse co-ordinate in it but wanted to know if its possible using above approach

        jsulmJ 1 Reply Last reply
        0
        • KiraK Kira

          @jsulm : thanks for the reply it worked like a charm.
          The issue got resolved now i can draw rubberband easily.

          The rubbandband get drawn easily when i assign QRubberband to main window widget.
          But when i try to assign it to QLabel there is issue with the drawing of rubberband.
          I search on the above topic and came to know it is because of difference of the co-ordinates of window and label and i have to map it globally using the below link:
          https://stackoverflow.com/questions/16527248/qrubberband-on-a-definite-label
          But the problem still exist and i am not able to draw the rectangle.
          //Example code:
          void MainWindow::mousePressEvent(QMouseEvent *event){

          mypoint = ui->label->mapFromGlobal(this->mapToGlobal(event->pos()));
          // mypoint = ui->label->mapFrom(this, event->pos());
          // mypoint = this->mapTo(ui->label, event->pos());
          if(rubberBand == 0) // You should add this to not have a memory leak
              rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->label_2);//new rectangle band
          rubberBand->setGeometry(QRect(mypoint, ui->label_2->size()));
          rubberBand->show();
          

          }
          After following the above approach,
          When i click the mouse inside the label the whole rubberband gets drawn without even moving the cursor.

          Another approach would be making a custom QLabel and implement the mouse co-ordinate in it but wanted to know if its possible using above approach

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @Kira said in Draw rectangle on QLabel:

          void MainWindow::mousePressEvent(QMouseEvent *event)

          You're overwriting mousePressEvent in MainWindow but actually you should do it only for the label, right? You should subclass QLabel and override mousePressEvent there.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • KiraK Offline
            KiraK Offline
            Kira
            wrote on last edited by
            #10

            @jsulm : Yes you are correct. But is there any way in which i can implement it in mouse event of the main window instead of creating the subclass of the QLabel as mentioned in the link by mapping the co-ordinates.

            jsulmJ 1 Reply Last reply
            0
            • KiraK Kira

              @jsulm : Yes you are correct. But is there any way in which i can implement it in mouse event of the main window instead of creating the subclass of the QLabel as mentioned in the link by mapping the co-ordinates.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @Kira Well, you need to check whether the user actually clicked inside the label or not.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • KiraK Offline
                KiraK Offline
                Kira
                wrote on last edited by
                #12

                @jsulm : can you please elaborate the approach. Because currently i am clicking inside the qlable and drawing the rubberband

                mrjjM 1 Reply Last reply
                0
                • KiraK Kira

                  @jsulm : can you please elaborate the approach. Because currently i am clicking inside the qlable and drawing the rubberband

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #13

                  @Kira

                  Hi
                  If you get mouseEvents to mainwindow, the x,y coordinates will be for the main window.
                  so when you press buttons, you need to use something like
                  http://doc.qt.io/qt-5/qapplication.html#widgetAt
                  and check if over the label.

                  But why do you want this ?
                  It will just be more complex than letting QLabel do it
                  You will also need to map coordinates as else it wont draw where u press as mainwindows x,Y is not the same
                  for label. so you must alter the x,y to draw inside QLabel.

                  1 Reply Last reply
                  1
                  • KiraK Offline
                    KiraK Offline
                    Kira
                    wrote on last edited by
                    #14

                    @jsulm @mrjj : Guys thanks for your support. Got the issue resolved.
                    Actually there was issue with the mouse drag functionality where i was passing the event of the main widget.
                    It got resolved by mapping the coordinates of the mainwindow to global coordinate system.

                    Will implement the functionality by creating subclass of the label and implement the rubberband as stated earlier as the correct approach.

                    Just wanted to see whether the implementation is possible by this approach.

                    Thanks again for your help and support.

                    1 Reply Last reply
                    1
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      Super :)
                      for at quick start, u could use
                      https://wiki.qt.io/Clickable_QLabel

                      1 Reply Last reply
                      0
                      • KiraK Offline
                        KiraK Offline
                        Kira
                        wrote on last edited by
                        #16

                        @mrjj : thanks buddy.
                        Best supportive forum with best people and special thanks to @SGaist

                        1 Reply Last reply
                        1

                        • Login

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