0

如何自定义 QPainterPath 的绘图算法?

我想通过只绘制可见元素来加速我的应用程序。

我正在从 QPainterPath 绘制实时绘图,并希望这样做:

  1. QPainterPath 最左边可见元素的二分搜索。
  2. QPainterPath 最右边可见元素的二进制搜索。
  3. 仅绘制可见元素。

我想我应该通过自定义 QWidget 的 QPaintEngine QWidget::paintEngine()

QPaintEngine::drawPath(...)默认实现什么也不做

我是对还是错?

更新:

谢谢你的回应,科英。

我刚刚尝试过这种方式:

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);

一切都很好!对于任何元素计数,我都有固定的时间开销。

你有什么建议可以加快我的代码速度吗?

UDPATE:

读取 QPainterPath 并从另一个线程向其中添加元素时出现错误。有时应用程序会因调用堆栈而崩溃:

QtCored4.dll!qt_message_output(QtMsgType msgType, const char * buf) 第 2240 行 C++ QtCored4.dll!qt_message(QtMsgType msgType, const char * msg, char * ap) 第 2298 行 + 0x12 字节 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 字节 C++ MyPlot.dll!MyPlot::paintEvent(QPaintEvent * event) 第 172 行 + 0x13 字节 C++

在调用 elementAt(i) 时,i == 4303 并且有 5459 个元素。

也许元素计数对于崩溃的时刻是不真实的?

当第一个线程崩溃时,第二个线程可以修改元素计数吗?

我的线程是这样工作的:我的 gui 线程只使用只读方法。我的第二个线程每秒增加 1-5 千点。

4

1 回答 1

1

QPaintEngine是底层的、特定于平台的绘画引擎的抽象接口,即 Windows 上的 GDI、Linux 上的 X、Mac 上的 cocoa 等等……所以这不是你要找的。

您应该做的是编辑您的QPainterPath以删除您不想绘制的元素。QPainterPath只是一个原语列表,有点像 SVG,而不是某种位图。

于 2012-03-06T09:43:09.680 回答