QGraphicsViewWhen the QGraphicsView ‘s scale function is enlarged enough, the rendered QGraphicsPixmapItem image will extend beyond the pixel grid.
-
ui->graphicsView->setRenderHint(QPainter::Antialiasing, false);
drawWangGe();
drawTuoYuan();
ui->graphicsView->setScene(&scene);
ui->graphicsView->show();void QGraphicsViewForm::wheelEvent(QWheelEvent *event)
{
if (event->angleDelta().y() > 0)
{
scale(1.1, 1.1);
update();
}
else
{
scale(1/1.1, 1/1.1);
update();
}
}void MainWindow::drawWangGe()
{
int width=800;
int height=600;
scene.setSceneRect(0, 0, width,height);
int gridWidth = width;
int gridHeight = height;
int numOfColumns = width;
int numOfRows = height;
QPen pen(Qt::red);
pen.setWidthF(1);
pen.setCosmetic(true);
pen.setStyle(Qt::SolidLine);
double cellWidth = gridWidth / static_cast<double>(numOfColumns);
double cellHeight = gridHeight / static_cast<double>(numOfRows);
for (int row = 0; row <= numOfRows; ++row)
{
double x1 = 0;
double y1 = row * cellHeight;
double x2 = gridWidth;
double y2 = row * cellHeight;
QGraphicsLineItem* line = new QGraphicsLineItem(x1, y1, x2, y2);
line->setPen(pen);
scene.addItem(line);
line->setZValue(1);
}
for (int column = 0; column <= numOfColumns; ++column)
{
double x1 = column * cellWidth;
double y1 = 0;
double x2 = column * cellWidth;
double y2 = gridHeight;
QGraphicsLineItem* line = new QGraphicsLineItem(x1, y1, x2, y2);
line->setPen(pen);
scene.addItem(line);
line->setZValue(1);
}
}void MainWindow::drawTuoYuan()
{QPixmap pixmap(200, 150); pixmap.fill(Qt::transparent); QPainter painter(&pixmap); painter.setRenderHint(QPainter::Antialiasing, false); painter.setBrush(Qt::blue); painter.setPen(Qt::blue); painter.drawEllipse(0, 0, 200, 150); painter.end(); QGraphicsPixmapItem* pixmapItem = new QGraphicsPixmapItem; pixmapItem->setPixmap(pixmap); pixmapItem->setZValue(0.5); pixmapItem->setPos(QPointF(300,300)); scene.addItem(pixmapItem);
}
When the above code is incorporated into the project and enlarged to a certain extent, it will produce the phenomenon as shown in my screenshot. How can this be resolved?