开发者

Optimized Line drawing in QT

开发者 https://www.devze.com 2023-01-24 22:21 出处:网络
I am new to QT. i am working on the Graphics. i am using QWidget for drawing graphics(For drawing graphics in QWidget paint event). i need to draw background and foreground graphics. Background is f

I am new to QT. i am working on the Graphics.

i am using QWidget for drawing graphics(For drawing graphics in QWidget paint event). i need to draw background and foreground graphics. Background is fixed graphics. foregrounds i am drawing lines.

Each 100 millisecond i need to draw 20points. This drawing time is 8 sec. Total i need to draw 1600 points (total points represents the contentious line).

i am using QTimer to invoke this drawing in each 100ms. first few drawing drawn very fast. in the middle of the drawing it's become slow.

the problem is i need to draw all the foreground and background in each开发者_如何学Python 100ms.

Please help me to fix the problem. if any one have sample code please provide. Thanks in advance.

Is there any way to draw only partial area ie. only particular modified region of the graphics?


QPainter-drawing can be very slow without hardware support. Using QGraphicsView won't help if all lines are visible, since it internally uses QPainter anyway.

If you just have to draw 20 new points (or lines) per update and per update background gets cleared so you have to render everything again, there are few things you could try:

1) Disable background autofill. See: QWidget::autoFillBackground Add something like this to your widget init:

setAutoFillBackground(false);
setAttribute(Qt::WA_OpaquePaintEvent, true);
setAttribute(Qt::WA_NoSystemBackground, true);

Now on the first update render background and first lines. For next updates just skip rendering background and render only new lines.

2) Use double buffering. For example, create QImage of the size of your widget.

.h
private:
  QImage m_targetImage;


.cpp
  // constructor
    m_targetImage = QImage(width(), height(), QImage::Format_ARGB32); 

  // paint event

   // draw to image
   QPainter p;
   p.begin(&m_targetImage);
   static bool firstUpdate = true;
   if (firstUpdate)
   { 
    // draw background)
    p.drawImage(...);
    firstUpdate = false;
   }

   // draw latest lines
   p.drawLines(....);
   p.end();

   // draw image in widget paint
   QPainter painter;
   painter.begin(this);
   painter.drawImage(0, 0, m_targetImage);
   painter.end();

3) Use QGLWidget if possible. Inherit your widget from QGLWidget instead of QWidget. This method doesn't work on all platforms and speed increase might not be enough. Also using OpenGL brings all kind of new problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号