我正在实现一个平铺渲染系统(以避免GL_MAX_VIEWPORT_DIMS
- 限制)。我需要支持QWidget
- 覆盖,并且必须为小部件实现平铺渲染。计划是通过使用正确的转换创建QPainter
并使用“已转换的”QWidget::render
调用QPainter
来实现此目的。
我有一些测试代码可以做到这一点:
int w = 24;
int h = 24;
QImage tiles[4];
QPushButton btn;
btn.resize(w, h);
btn.move(w/2, h/2);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
int tileidx = i + 2*j;
tiles[tileidx] = QImage(QSize(w, h), QImage::Format_ARGB32);
tiles[tileidx].fill(0);
QPainter painter(&tiles[tileidx]);
painter.setViewport(i*w,i*h, w,h); // or setWindow()
painter.save();
painter.translate(btn.pos());
btn.render(&painter);
painter.restore();
}
}
for (int i = 0; i < 4; ++i) {
tiles[i].save(QString("c:/temp/tile%0.bmp").arg(i));
}
我希望在每个图块中呈现按钮的四分之一,但结果却完全不同:
上面的瓷砖数字是:
0 2
1 3
如何正确设置QPainter(或实现我需要的内容)?
答案 0 :(得分:0)
我最终绕过QPainter
,自己进行计算并直接致电QWidget::render()
:
QRect viewport(i*w,j*h, w,h);
QRect intersection = viewport.intersected(widget->frameGeometry());
if (!intersection.isEmpty()) {
QPoint target = intersection.topLeft() - viewport.topLeft();
QPoint topLeft = widget->mapFromGlobal(intersection.topLeft());
QPoint bottomRight = widget->mapFromGlobal(intersection.bottomRight());
widget->render(&paintDevice, target, QRegion(intersection));
}