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. How to Draw Any Shape on QLabel Image
Forum Updated to NodeBB v4.3 + New Features

How to Draw Any Shape on QLabel Image

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.6k Views 1 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.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by
    #1

    I am using QLabel For Image Displaying Now I want To Draw Shape On Image
    Like Rectangle Or Line

    plz Help Me For Slove This Problem

    jsulmJ 1 Reply Last reply
    0
    • Ketan__Patel__0011K Ketan__Patel__0011

      I am using QLabel For Image Displaying Now I want To Draw Shape On Image
      Like Rectangle Or Line

      plz Help Me For Slove This Problem

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

      @Ketan__Patel__0011 Subclass QLabel and override paintEvent where you then use QPainter to draw what ever you need.
      Here an example: https://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html

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

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

        Hi
        You can also draw on a pixmap and show in label.

        However, if you plan to let user draw the rect or need to be able to erase them again or similar then
        subclassing is the way to go.

        int GetBarHeight(int MAX) {
        return rand() % (MAX-5)+5;
        }

        void MainWindow::on_pushButton_released() {
        int h = ui->label->height();
        int w = ui->label->width();
        QPixmap pix(w, h);
        QPainter paint(&pix);
        // paint the image over the pix map here.
        pix.fill( Qt::white );
        paint.setPen(QColor(0, 0, 0, 255));
        int y = 0;
        int x = 0;
        int bw = 10; // bar width
        for (int barcount = 0; barcount < 12; ++barcount) {
        paint.setBrush(QColor(255-x, 34+x, 255, 255));
        paint.drawRect(x, h - GetBarHeight(h), bw, h );
        x += bw + 4;
        }
        paint.end();
        ui->label->setPixmap(pix);
        }

        alt text

        1 Reply Last reply
        3
        • jsulmJ jsulm

          @Ketan__Patel__0011 Subclass QLabel and override paintEvent where you then use QPainter to draw what ever you need.
          Here an example: https://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html

          Ketan__Patel__0011K Offline
          Ketan__Patel__0011K Offline
          Ketan__Patel__0011
          wrote on last edited by
          #4

          @jsulm i want to draw Rectangle Over The image using Mouse

          mrjjM jsulmJ 2 Replies Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            @jsulm i want to draw Rectangle Over The image using Mouse

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

            @Ketan__Patel__0011

            ok then you should create your own label subclass for that as that is much easier than doing it via MainWIndow.

            You can use this class
            https://doc.qt.io/qt-5/qrubberband.html

            to make it possible to drag out a rect very easy if you don't want to hand program that effect your self.

            There is example here of such widget
            http://mangoprojects.info/c/qtc-drawing-a-line-or-rectangle-dynamically/

            1 Reply Last reply
            2
            • Ketan__Patel__0011K Ketan__Patel__0011

              @jsulm i want to draw Rectangle Over The image using Mouse

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

              @Ketan__Patel__0011 Then, in addition what I already wrote, you will need to override mousePressEvent, mouseMoveEvent and mouseReleaseEvent

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

              1 Reply Last reply
              2

              • Login

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