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 shapes bellow a specific element using its position with QPainter and draw functions ?
QtWS25 Last Chance

How to draw shapes bellow a specific element using its position with QPainter and draw functions ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpainterpainteventdrawing
2 Posts 2 Posters 733 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.
  • DSpiderD Offline
    DSpiderD Offline
    DSpider
    wrote on last edited by
    #1

    I have a QWT plot in the mainwindow and I am trying to draw some shapes just bellow. Here is what I am using:

    void MainWindow::paintEvent(QPaintEvent *event)
    {
        // unuse
        Q_UNUSED(event);
    
        // pass "this" pointer to painter
        QPainter painter(this);
    
        // setPen
        // QPen can take "Qt::black" directly as first arg (without QBrush() constructor)
        painter.setPen(QPen(Qt::black));
    
        // draw line
        painter.drawRect(10, 10, 10, 10);
    }
    

    I know we can play with the coordinates until I get to the desired position, but is there another way using the coordinate of the QWTPlot ?

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

      Hi
      Well to get below the QWTPlot plot would
      be QWTPlotptr->y() + QWTPlotptr->height()
      However, a more solid approach could be to use a QLabel as
      a canvas using a pixmap. That way, its very easy to position even if using layouts to make
      GUI auto adjust to different screen sizes.

      void PaintShapes() 
      {
      // take size of label
        int h = ui->label->height();
        int w = ui->label->width();
      // make a pixmap of the wanted size
        QPixmap pix(w, h);
      // assign painter to it so we can paint on it
        QPainter paint(&pix);
      // fill it
        pix.fill( Qt::white );
      // paint on canvas
        paint.setPen(QColor(0, 0, 0, 255));
       paint.drawRect(0, 0, w, h);
      ..other paint operations-..
       // set the pixmap to the label so its shown.
        ui->label->setPixmap(pix);
       }
      
      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