简体   繁体   中英

QPaintEvent - updating only a region of the screen

in my code I'm trying to draw points every time my app gets GPS coordinates. The points are moved by "i" pixels to the right and down. I would like them to stay there drawn but it seems like the whole screen is always updated, even though I'm using a QRegion parameter which is meant to specify an area on the screen which should be updated. Is there anyone who could help, please? I'm really new at this and have no idea what is wrong.

Here is the class which handles this action:

GameField::GameField(QWidget *parent)
    : QWidget(parent)
{
   i=5;
   j=0;
   pen=new QPen(Qt::black, 1, Qt::SolidLine);
   painter= new QPainter(this);
}

void GameField::paintEvent(QPaintEvent *event)
{

painter.setPen(pen);
painter.drawPoint(i,i,1,1);


  }

void GameField::positionUpdated(QGeoPositionInfo position) {
  QGeoCoordinate coordinates;
  if (position.isValid()) {
    coordinates = position.coordinate();
  }
i=i+5;
QRegion region(QRect(i,i,5,5));
this->update(region);
  }

Your paintEvent must be able to repaint the entire widget. For example, if a user minimizes and then maximizes your application, the entire widget must be repainted. The QPaintEvent::region can be used to suppress some of that painting if those paint operations are expensive. Here is a sample. (This is only a proof of concept. There are a lot of "bad ideas" in the code below, not the least of which is that in this case the cost to test the region is not worth it. But it at least shows the logic.)

#include <QtGui>

class PaintWidget : public QWidget {
  Q_OBJECT
public slots:
  void AddPoint() {
    QPoint point(rand() % width(), rand() % height());
    points_ << point;
    update(point.x() - 3, point.y() - 3, 6, 6);
  }
protected:
  void paintEvent(QPaintEvent *event) {
    qDebug() << Q_FUNC_INFO;
    QPainter painter(this);
    painter.setPen(Qt::SolidLine);
    foreach (QPoint point, points_) {
      if (event->region().contains(point)) {
        qDebug() << "drawing point:" << point;
        painter.drawEllipse(point, 2, 2);
      }
    }
  }
private:
  QVector<QPoint> points_;
};

int main(int argc, char** argv) {
  QApplication app(argc, argv);

  PaintWidget w;
  w.show();

  QTimer timer;
  timer.connect(&timer, SIGNAL(timeout()), &w, SLOT(AddPoint()));
  timer.start(1000);

  return app.exec();
}

#include "main.moc"

Notice how if you leave the program visible, it paints individual dots with each paint event. But if you pass another window in front of yours, or minimize/maximize the application, many points are painted in one paint event.

For drawing points, it is hard to see how considering the region could be of much benefit. But if your geographic points formed a QPainterPath or something like that, you may get some performance benefit.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM