antialiasing leaves thin line between adjacent widgets
I have two widgets that both have a dark background of the same color. These widgets lie in adjacent cells within a QGridLayout
which has ContentsMargins
and spacing of 0 on all sides. I use a custom QStyle
-derived class for drawing.
If I don't use antialiasing, everything looks as expected - the two widgets' backgrounds merge into one continuous dark area. Turning on antialiasing (via painter->setRenderHint(QPainter::Antialiasing, true);
) leaves a thin (1px) white line between these two widgets.
Is it possible to somehow get rid of this line? Turning off antialiasing altogether is not an option as these two widgets have rounded corners and these look bad without.
EDIT
I made up a "minimal" example now:
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QPainter>
class foo : public QWidget
{
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
int x1, y1, x2, y2;
开发者_StackOverflow 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();
}
I am working on ubuntu with Qt 4.7.3.
Try to shift by half-pixel, either when creating the path or by translate(0.5, 0.5)
after setting the render hints.
If you know where these unsightly boundary lines are going to occur, you can just overwrite them with a straight gray line (which might need to be two or three pixels wide if anti-aliasing is still enabled). Not very elegant, but it does the job.
精彩评论