QT QPainter рисует на QPixmap с координатами вне растрового изображения - PullRequest
0 голосов
/ 13 февраля 2019

Что происходит, когда я рисую на 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...
  • Это разрешено?
  • Если это так, понимает ли художник, что это бесполезная операция и ничего не делает, или она все равно проходит через движения иПотеря мощности обработки?

Я собирался написать некоторый код, который проверяет, находится ли объект для рисования внутри границ или нет, но если художник пропускает рисование вне границ объектов, тогда это было бы излишним.

1 Ответ

0 голосов
/ 14 февраля 2019

Рисование - это одна из основных задач, которая требует времени, поэтому она должна быть оптимизирована, и, как экспериментально подтверждено в следующем примере, 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

В предыдущем примере наблюдается корреляция с окрашенной областью и временем, затраченным на указанную операцию.

...