C++ 抗锯齿在相邻的小部件之间留下细线
我有两个小部件,它们都有相同颜色的深色背景。这些小部件位于C++ 抗锯齿在相邻的小部件之间留下细线,c++,qt,antialiasing,C++,Qt,Antialiasing,我有两个小部件,它们都有相同颜色的深色背景。这些小部件位于QGridLayout中的相邻单元格中,该布局具有ContentsMargins,所有边的间距均为0。我使用自定义的QStyle派生类进行绘图 如果我不使用抗锯齿,一切看起来都和预期的一样——两个小部件的背景合并成一个连续的黑暗区域。启用抗锯齿(通过painter->setRenderHint(QPainter::antialasing,true);)会在这两个小部件之间留下一条细细的(1px)白线 有没有可能以某种方式摆脱这条线?完全关
QGridLayout
中的相邻单元格中,该布局具有ContentsMargins
,所有边的间距均为0。我使用自定义的QStyle
派生类进行绘图
如果我不使用抗锯齿,一切看起来都和预期的一样——两个小部件的背景合并成一个连续的黑暗区域。启用抗锯齿(通过painter->setRenderHint(QPainter::antialasing,true);
)会在这两个小部件之间留下一条细细的(1px)白线
有没有可能以某种方式摆脱这条线?完全关闭抗锯齿不是一个选项,因为这两个小部件都有圆角,没有圆角看起来很糟糕
编辑
我现在制作了一个“最小”示例:
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QPainter>
class foo : public QWidget
{
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
int x1, y1, x2, y2;
int radius = 20;
int diam = 2 * radius;
rect().getCoords(&x1, &y1, &x2, &y2);
QPainterPath path; // This will be a rounded rectangle.
path.moveTo(x1 + radius, y2);
path.lineTo(x2 - radius, y2);
path.arcTo(x2 - diam, y2 - diam, diam, diam, 270.0, 90.0);
path.lineTo(x2, y1 + radius);
path.arcTo(x2 - diam, y1, diam, diam, 0.0, 90.0);
path.lineTo(x1 + radius, y1);
path.arcTo (x1, y1, diam, diam, 90.0, 90.0);
path.lineTo(x1, y2 - radius);
path.arcTo (x1, y2 - diam, diam, diam, 180.0, 90.0);
path.closeSubpath();
painter.setPen(Qt::gray);
// Comment out the following line and the rounded rectangles
// will not have a thin boundary of background color between them
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, Qt::gray);
painter.drawPath(path);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGridLayout *l = new QGridLayout;
l->setContentsMargins(0,0,0,0);
l->setSpacing(0);
foo *c1 = new foo;
foo *c2 = new foo;
l->addWidget(c1, 0, 0);
l->addWidget(c2, 0, 1);
QWidget *w = new QWidget;
w->setMinimumSize(500,250);
w->setLayout(l);
w->show();
return a.exec();
}
#包括
#包括
#包括
#包括
类foo:publicqwidget
{
受保护的:
无效paintEvent(QPaintEvent*){
油漆工(本);
int-x1,y1,x2,y2;
int半径=20;
内径=2*半径;
rect().getCoords(&x1,&y1,&x2,&y2);
QPainterPath;//这将是一个圆角矩形。
路径移动到(x1+半径,y2);
线路到(x2-半径,y2);
弧径(x2-直径,y2-直径,直径,直径,270.0,90.0);
线路到(x2,y1+半径);
弧径(x2-直径,y1,直径,直径,0.0,90.0);
线路到(x1+半径,y1);
路径弧(x1,y1,直径,直径,90.0,90.0);
lineTo路径(x1,y2-半径);
弧径(x1,y2-直径,直径,直径,180.0,90.0);
path.closeSubpath();
画师:setPen(Qt::灰色);
//注释掉下面的一行和圆角矩形
//它们之间不会有薄的背景色边界
painter.setRenderHint(QPainter::抗锯齿,true);
painter.fillPath(路径,Qt::灰色);
绘制路径(路径);
}
};
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QGridLayout*l=新的QGridLayout;
l->setContentsMargins(0,0,0,0);
l->setspace(0);
foo*c1=新的foo;
foo*c2=新的foo;
l->addWidget(c1,0,0);
l->addWidget(c2,0,1);
QWidget*w=新的QWidget;
w->设置最小尺寸(500250);
w->设置布局(l);
w->show();
返回a.exec();
}
我正在使用Qt 4.7.3开发ubuntu。如果你知道这些难看的边界线将出现在哪里,你可以用一条灰色直线覆盖它们(如果仍然启用抗锯齿,可能需要两到三个像素宽)。不是很优雅,但它可以完成任务。在创建路径时,或者在设置渲染提示后,通过
平移(0.5,0.5)
尝试移动半像素。我对qt了解不够,不知道它是否受支持,但您需要的是复合形状。如果它不受支持,基本上你可以使用一个好主意,但问题是AGG似乎没有很好地集成到Qt中。我读了一些usenet的帖子,有人在那里尝试过,但似乎没有太多的结果,至少没有什么好的表现。不过还是要谢谢你,我会一直盯着你看的。是的,那一定会成功的。然而,正如你所说,它不是很优雅。因此,我希望有人有一个更优雅的解决方案。好吧,在我的最小示例中,这确实起到了作用。你知道为什么会出现这种情况吗?我的完整代码也已经开始工作了,除了一些其他的像素移位外,这个.5px移位确实起到了作用。非常感谢你!这是抗锯齿渲染的一部分。有关详细信息,请参阅。