简体   繁体   English

QPaintEvent - 只更新屏幕的一个区域

[英]QPaintEvent - updating only a region of the screen

in my code I'm trying to draw points every time my app gets GPS coordinates.在我的代码中,每次我的应用程序获取 GPS 坐标时,我都会尝试绘制点。 The points are moved by "i" pixels to the right and down.这些点向右和向下移动“i”个像素。 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.我希望它们留在那里绘制,但似乎整个屏幕总是更新,即使我使用的是 QRegion 参数,该参数旨在指定屏幕上应该更新的区域。 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.您的paintEvent必须能够重新绘制整个小部件。 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.如果这些绘制操作很昂贵,则QPaintEvent::region可用于抑制某些绘制。 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.但是如果您的地理点形成了QPainterPath或类似的东西,您可能会获得一些性能优势。

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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