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 with QPainter in a label

Draw with QPainter in a label

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 3.5k 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.
  • I Offline
    I Offline
    Isidro Perla
    wrote on last edited by
    #1

    Hello, i want to know how can I draw a rectangle in a ui label, for exaple ui->label_2, Here is current code, but only draw in the window.

    void MainWindow::paintEvent(QPaintEvent *event){

    QPainter painter(this);
    
    QPen pen;
    pen.setColor(Qt::blue);
    painter.drawRect(QRect(80,120,200,100));
    

    }

    How Can I do to draw in ui->label_2 with QPainter????

    JonBJ 1 Reply Last reply
    0
    • I Isidro Perla

      Hello, i want to know how can I draw a rectangle in a ui label, for exaple ui->label_2, Here is current code, but only draw in the window.

      void MainWindow::paintEvent(QPaintEvent *event){

      QPainter painter(this);
      
      QPen pen;
      pen.setColor(Qt::blue);
      painter.drawRect(QRect(80,120,200,100));
      

      }

      How Can I do to draw in ui->label_2 with QPainter????

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Isidro-Perla
      By (subclassing and overriding) your QLabel's paintEvent, instead of that of the MainWindow. Every QWidget has its own https://doc.qt.io/qt-5/qwidget.html#paintEvent.

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

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

        void MainWindow::on_pushButton_released() {
          int h = ui->label->height();
          int w = ui->label->width();
          QPixmap pix(w, h);
          QPainter paint(&pix);
          pix.fill( Qt::white );
          paint.setPen(QColor(0, 0, 0, 255));
          paint.drawRect(QRect(80,120,200,100));
          ui->label->setPixmap(pix);
        }
        
        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