Qt 当我启用抗锯齿时,为什么会在QGraphicsView中看到pixmap图块之间的间隙
我正在使用QGraphicsView框架显示较小QPixmap分幅的大图像。我还想启用抗锯齿,因为场景将包含行项目。启用抗锯齿时,为什么会看到瓷砖之间的间隙Qt 当我启用抗锯齿时,为什么会在QGraphicsView中看到pixmap图块之间的间隙,qt,qgraphicsview,qpixmap,Qt,Qgraphicsview,Qpixmap,我正在使用QGraphicsView框架显示较小QPixmap分幅的大图像。我还想启用抗锯齿,因为场景将包含行项目。启用抗锯齿时,为什么会看到瓷砖之间的间隙 class MainWindow : public QWidget { public: MainWindow(QWidget *parent = 0); protected: void resizeEvent(QResizeEvent*); private: QGraphicsScene* _scene;
class MainWindow : public QWidget
{
public:
MainWindow(QWidget *parent = 0);
protected:
void resizeEvent(QResizeEvent*);
private:
QGraphicsScene* _scene;
QGraphicsView* _view;
qreal _scale;
static const int _imageWidth = 512;
static const int _imageHeight = 128;
};
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
_scene = new QGraphicsScene(this);
_view = new QGraphicsView(_scene, this);
//this causes gaps to appear ?
_view->setRenderHints(QPainter::Antialiasing);
_scene->setBackgroundBrush( QBrush( QColor( Qt::lightGray ) ) );
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(_view);
setWindowTitle(QString("GapsBetweenTiles- QT Version %1")
.arg(QT_VERSION_STR));
QImage img = QImage(_imageWidth, _imageHeight, QImage::Format_RGB32);
img.fill(QColor(00, 50, 50));
int offset = 0;
for (int k=0; k < 10; ++k) {
QGraphicsPixmapItem* pixitem = _scene->addPixmap(
QPixmap::fromImage(img));
pixitem->setTransformationMode(Qt::SmoothTransformation);
pixitem->setPos(0, offset);
offset += _imageHeight;
}
_scale = 1.0;
}
void MainWindow::resizeEvent(QResizeEvent* )
{
int scaledWidth = (qreal)_view->width() -
_view->verticalScrollBar()->width() ;
qreal scale = (qreal)scaledWidth / (qreal)_imageWidth;
qreal scaleMult = scale / _scale;
_view->scale(scaleMult, scaleMult);
_scale = scale;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
class主窗口:公共QWidget
{
公众:
主窗口(QWidget*父窗口=0);
受保护的:
void resizeEvent(QResizeEvent*);
私人:
QGraphicscene*_场景;
QGraphicsView*\u视图;
qreal_量表;
静态常数int_imageWidth=512;
静态常数int_imageHeight=128;
};
主窗口::主窗口(QWidget*父窗口)
:QWidget(父项)
{
_场景=新的Qgraphicscene(此);
_视图=新的QGraphicsView(_场景,此);
//这会导致出现间隙吗?
_视图->SetRenderInts(QPaint::抗锯齿);
_场景->背景笔刷(QBrush(QColor(Qt::浅灰色));
QHBoxLayout*布局=新的QHBoxLayout(本);
布局->添加小部件(\u视图);
setWindowTitle(QString(“间隙-QT版本%1”)
.arg(QT_VERSION_STR));
QImage img=QImage(_imageWidth,_imageHeight,QImage::Format_RGB32);
图像填充(QColor(00,50,50));
整数偏移=0;
对于(int k=0;k<10;++k){
QGraphicsPixmapItem*pixitem=\u场景->添加pixmap(
QPixmap::fromImage(img));
pixitem->setTransformationMode(Qt::SmoothTransformation);
pixitem->setPos(0,偏移量);
偏移量+=\u图像高度;
}
_比例=1.0;
}
void主窗口::resizeEvent(QResizeEvent*)
{
int scaledWidth=(qreal)\u视图->宽度()-
_视图->垂直滚动条()->宽度();
qreal标度=(qreal)标度宽度/(qreal)\u图像宽度;
qreal scaleMult=标度/标度;
_查看->缩放(缩放、缩放);
_比例=比例;
}
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
w、 show();
返回a.exec();
}
当缩放图像高度(\u imageHeight*scale
)变为分数时,会出现间隙。
每个QGraphicsPixmapItem
都作为单独的对象绘制。如果此类对象具有分数高度,则启用抗锯齿时,边界将平滑(部分绘制分数边界线)
在您的案例中,有三种可能的间隙布局:
- 如果缩放图像高度为整数,则没有间隙
- 如果高度的分数部分为0.5,则1个有间隙对象和1个无间隙对象的周期序列
- 如果分数部分为0.25或0.75,则3个有间隙对象和1个无间隙对象的周期序列;此处,第二个间隙比第一个和第三个间隙亮
resizeEvent
中添加以下行来验证:
scaledWidth = (scaledWidth / 4) * 4;
顺便说一下,通过删除以下行,您可以仅对
QGraphicsPixmapItem
对象禁用抗锯齿:
pixitem->setTransformationMode(Qt::SmoothTransformation);