QT QPainter绘制到QPixmap上,其坐标位于像素图之外

时间:2019-02-13 15:45:35

标签: qt coordinates qpainter qpixmap

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

QPainter
  • 可以吗?
  • 如果是这样,画家是否意识到这是无用的操作并且什么也不做,还是无论如何都要经过运动并浪费处理能力?

我正要编写一些代码来检查要绘制的对象是否在边界之内,但是如果画家无论如何都跳过边界的绘制,那将是多余的。

1 个答案:

答案 0 :(得分: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

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