反走样在相邻的小部件之间留下了细线。

3
我有两个小部件,它们都具有相同颜色的深色背景。这些小部件位于一个QGridLayout中的相邻单元格中,该单元格在所有边缘上均具有ContentsMargins和0间距。我使用自定义的QStyle派生类进行绘制。
如果我不使用抗锯齿,一切都看起来正常——两个小部件的背景合并为一个连续的黑暗区域。通过painter->setRenderHint(QPainter::Antialiasing, true);打开抗锯齿会在这两个小部件之间留下一条细(1像素)的白线。
有没有可能以某种方式摆脱这条线?完全关闭抗锯齿不是选项,因为这两个小部件都有圆角,没有抗锯齿看起来很糟糕。
编辑:
我现在列出了一个“最小”示例:
#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();
} 

我正在使用Qt 4.7.3在Ubuntu上工作。


我不太了解Qt是否支持,但你需要的是复合形状。如果不支持,你可以使用antigrain - user707582
基本上是个好主意,但问题在于AGG似乎不能很好地集成到Qt中。我读了几篇usenet帖子,人们尝试过它,但似乎没有太多的结果,至少没有良好的性能。但无论如何,还是谢谢,我会保持关注的。 - arne
2个回答

1
尝试通过半像素移位来创建路径,或在设置渲染提示后使用translate(0.5, 0.5)进行移位。

好的,在我的最小化示例中,这确实起到了作用。你知道为什么会这样吗? - arne
我成功地运行了我的完整代码,其中这个0.5像素的移动真的起到了关键作用,还有一些其他像素的移动。非常感谢! - arne
这是抗锯齿渲染的一部分。请参阅http://doc.qt.nokia.com/latest/qrect.html#rendering了解详细信息。 - Ariya Hidayat

0

如果您知道这些不美观的边界线将出现在哪里,您可以用一条直灰线覆盖它们(如果抗锯齿仍然启用,则可能需要两到三个像素宽)。虽然不太优雅,但它能完成工作。


是的,那绝对可以解决问题。然而,正如您所说,它并不是很优雅。因此,我希望有人能提供更加优雅的解决方案。 - arne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接