Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Drawing a rectangle



  • I need to draw transparent rectangles on a blank image. Is there a way to do it more efficiently than what I already have?

    QImage img(this->size, QImage::Format_ARGB32);
    QPainter painter(&img);
    img.fill(QColor(0,0,0,0));
    painter.setOpacity(0.4);
    int size = tileSize - overLap;
    for (int i = 0; i < isShown.size(); i++) {
        for (int j = 0; j < isShown[i].size(); j++) {
            if (needToPaint(i,j)) {
                QColor color(getScore(i,j)*255,0,0);
                QRect dest = QRect(j*(size), i*(size), size, size);
                QPainterPath path;
                path.addRect(dest);
                painter.fillPath(path, color);
                painter.drawPath(path);
            }
        }
    }
    painter.end();
    

  • Qt Champions 2017

    QPainter::drawRect is probably a tad more efficient, but there might be other viable ideas depending on your data. For example you could paint opaque only instead of using transparency, as you already know what the background color is ...


Log in to reply