QT QPainter使用pixmap外的坐标绘制到QPixmap上

问题描述 投票:1回答:1

当我使用QPixmap使用像素图之外的坐标绘制到QPainter时会发生什么?例如:

QPixmap pixmap(500, 500);
QPainter painter(&pixmap);
painter.setPen(Qt::red);
painter.drawRect(600, 600, 100, 100); 
//Position 600, 600 is far outside the 500x500 pixmap...
  • 这是允许的吗?
  • 如果是这样,画家是否意识到这是一项无用的操作并且什么也不做,或者无论如何都要通过这些动作并浪费处理能力?

我打算编写一些代码来检查绘制的对象是否在边界内,但是如果画家跳过绘制出边界对象,那么这将是多余的。

qt coordinates qpainter qpixmap
1个回答
2
投票

绘画是消耗时间的主要任务之一,因此必须对其进行优化,并且通过以下示例进行实验验证,通过识别何时需要重新绘制来优化QPainter。

#include <QtWidgets>
#include <random>

struct Info{
    QRect r;
    double percentage;
    qint64 time;
};

static qint64 measure_task(const QSize & size, const QRect & rect, int times=1000){
    QPixmap pixmap(size);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    QElapsedTimer timer;
    timer.start();
    painter.setPen(Qt::red);
    for (int i=0;i<times;i++)
        painter.drawRect(rect);
    painter.end();
    return timer.elapsed();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    const QSize size(500, 500);
    const QRect rect({}, size);
    std::random_device rd;
    std::mt19937 gen(rd()) ;
    std::uniform_int_distribution<> dis(0, 600) ;
    std::vector<Info> infos;
    for(int i=0; i<50; i++){
        QRect r(dis(gen), dis(gen), 100, 100);
        QSize s = rect.intersected(r).size();
        Info info{r, s.width()*s.height()*1.0/(r.width()*r.height()), measure_task(size, r)};
        infos.push_back(info);
    }
    std::sort(infos.begin(), infos.end(), [](const Info & a, const Info & b) -> bool{
        return a.percentage < b.percentage;
    });
    for(const Info & info: infos){
        qDebug()<< info.r << "percentage:" << info.percentage << "time:" << info.time << "ms";
    }
    return 0;
}

输出:

QRect(463,576 100x100) percentage: 0 time: 0 ms
QRect(544,413 100x100) percentage: 0 time: 0 ms
QRect(223,539 100x100) percentage: 0 time: 0 ms
QRect(405,571 100x100) percentage: 0 time: 0 ms
QRect(33,502 100x100) percentage: 0 time: 0 ms
QRect(539,118 100x100) percentage: 0 time: 0 ms
QRect(576,205 100x100) percentage: 0 time: 0 ms
QRect(594,71 100x100) percentage: 0 time: 0 ms
QRect(386,535 100x100) percentage: 0 time: 0 ms
QRect(596,185 100x100) percentage: 0 time: 0 ms
QRect(343,525 100x100) percentage: 0 time: 0 ms
QRect(324,537 100x100) percentage: 0 time: 0 ms
QRect(43,525 100x100) percentage: 0 time: 0 ms
QRect(499,79 100x100) percentage: 0.01 time: 2 ms
QRect(292,482 100x100) percentage: 0.18 time: 3 ms
QRect(353,474 100x100) percentage: 0.26 time: 3 ms
QRect(442,451 100x100) percentage: 0.2842 time: 2 ms
QRect(457,296 100x100) percentage: 0.43 time: 5 ms
QRect(455,150 100x100) percentage: 0.45 time: 4 ms
QRect(203,450 100x100) percentage: 0.5 time: 5 ms
QRect(448,217 100x100) percentage: 0.52 time: 4 ms
QRect(47,437 100x100) percentage: 0.63 time: 4 ms
QRect(434,5 100x100) percentage: 0.66 time: 4 ms
QRect(419,406 100x100) percentage: 0.7614 time: 3 ms
QRect(215,417 100x100) percentage: 0.83 time: 5 ms
QRect(171,408 100x100) percentage: 0.92 time: 5 ms
QRect(304,180 100x100) percentage: 1 time: 8 ms
QRect(192,242 100x100) percentage: 1 time: 8 ms
QRect(295,162 100x100) percentage: 1 time: 8 ms
QRect(136,96 100x100) percentage: 1 time: 8 ms
QRect(348,243 100x100) percentage: 1 time: 8 ms
QRect(60,46 100x100) percentage: 1 time: 8 ms
QRect(125,281 100x100) percentage: 1 time: 8 ms
QRect(340,44 100x100) percentage: 1 time: 8 ms
QRect(107,204 100x100) percentage: 1 time: 8 ms
QRect(24,120 100x100) percentage: 1 time: 8 ms
QRect(246,271 100x100) percentage: 1 time: 8 ms
QRect(344,90 100x100) percentage: 1 time: 8 ms
QRect(239,329 100x100) percentage: 1 time: 8 ms
QRect(386,314 100x100) percentage: 1 time: 8 ms
QRect(241,218 100x100) percentage: 1 time: 8 ms
QRect(348,312 100x100) percentage: 1 time: 8 ms
QRect(187,315 100x100) percentage: 1 time: 8 ms
QRect(362,268 100x100) percentage: 1 time: 8 ms
QRect(112,43 100x100) percentage: 1 time: 8 ms
QRect(276,106 100x100) percentage: 1 time: 8 ms
QRect(378,201 100x100) percentage: 1 time: 8 ms
QRect(356,131 100x100) percentage: 1 time: 8 ms
QRect(308,269 100x100) percentage: 1 time: 8 ms
QRect(326,322 100x100) percentage: 1 time: 8 ms

在前面的例子中,观察到绘制区域和所述操作消耗的时间的相关性。

© www.soinside.com 2019 - 2024. All rights reserved.