Qt World Summit: Submit your Presentation

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

  • 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
        // pass "this" pointer to painter
        QPainter painter(this);
        // setPen
        // QPen can take "Qt::black" directly as first arg (without QBrush() constructor)
        // 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 ?

  • Lifetime Qt Champion

    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.