Question

How is it possible to customize the drawing algothithm of QPainterPath?

I would like to speed up my app by painting only visible elements.

I am drawing real-time plot from QPainterPath and would like to do this:

  1. Binary search of leftmost visible element of QPainterPath.
  2. Binary search of rightmost visible element of QPainterPath.
  3. Draw only visible elements.

I think I should customize QPaintEngine of my QWidget via QWidget::paintEngine().

But QPaintEngine::drawPath(...) default implementation does nothing .

Am I right or not?

UPDATE:

Thanks for respounce, Koying.

I've just tryed this way:

int minIndex = BinarySearchForMatchOrGreat(path, beginOffset);
int maxIndex = BinarySearchForMatchOrGreat(path, endOffset);
QPainterPath newPath;
for (int i = minIndex; i < maxIndex; i++)
{
    const QPainterPath::Element & element = path.elementAt(i);
    newPath.moveTo(element.x, element.y);
}
painter.drawPath(newPath);

All works nice! I have fixed time overhead for any element count.

Have you any suggestions to speed up my code?

UDPATE:

I have error while reading QPainterPath and adding elements to it from another thread. Sometimes app crashes with callstack:

QtCored4.dll!qt_message_output(QtMsgType msgType, const char * buf) Line 2240 C++ QtCored4.dll!qt_message(QtMsgType msgType, const char * msg, char * ap) Line 2298 + 0x12 bytes C++ QtCored4.dll!qFatal(const char * msg, ...) Line 2481 + 0xf bytes C++ QtCored4.dll!qt_assert(const char * assertion, const char * file, int line) Line 1999 + 0x16 bytes C++ QtGuid4.dll!QPainterPath::elementAt(int i) Line 405 + 0x36 bytes C++ MyPlot.dll!MyPlot::paintEvent(QPaintEvent * event) Line 172 + 0x13 bytes C++

while calling elementAt(i), i == 4303 and there are 5459 elements.

Maybe element count is not real for moment of crash?

Can second thread modify element count while first thread is crashing?

This how works my threads: My gui thread uses only read-only methods. My second thread adds 1-5 thousands point per second.

Was it helpful?

Solution

QPaintEngine is an abstract interface to the underlying, platform-specific painting engine, i.e. GDI on on Windows, X on Linux, cocoa on Mac, etc... so is not what you are looking for.

What you should do is edit your QPainterPath to remove the elements that you don't want to be painted. QPainterPath is just a list of primitives, a bit like SVG, not some kind of bitmap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top