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. Drawing a rectangle

Drawing a rectangle

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

    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();
    
    kshegunovK 1 Reply Last reply
    0
    • Jonathan LevanonJ Jonathan Levanon

      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();
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      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 ...

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3

      • Login

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