Qt抗锯齿设置不同的笔宽

时间:2019-01-04 18:45:19

标签: c++ qt antialiasing

我有一个qt应用,其中有一个很大的QGraphicsView,可在其中绘制网格。

此网格在单击鼠标时做出反应。基本上,如果您单击某个框->该框将变为红色。如果双击它->它会返回到原始状态(白色)。

这是我的QGraphicsView的实现:

#include "mapview.h"

// Constructors

mapview::mapview()
{
    setUpGui();
}

mapview::mapview(QWidget *parent) : QGraphicsView(parent)
{
    setUpGui();
}

// GUI setup

void mapview::setUpGui()
{
    scene = new QGraphicsScene(this);

    this->setScene(scene);
    this->setRenderHint(QPainter::Antialiasing);
//    this->setRenderHint(QPainter::HighQualityAntialiasing);
}

// Events

void mapview::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        lastPoint = event->pos();
        scribbling = true;
    }
}

void mapview::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && scribbling)
    {
        drawWall(event->pos());
    }
}

void mapview::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && scribbling)
    {
        drawWall(event->pos());
        scribbling = false;
    }
}

void mapview::mouseDoubleClickEvent(QMouseEvent *event)
{
    removeWall(event->pos());
}

// Drawing methods

void mapview::drawGrid(const int box_count)
{
    scene->clear();

    auto x = 0.0;
    auto y = 0.0;

    this->margin = 20.0;

    _width = this->width() - 2 * margin;
    _height = this->height() - 2 * margin;

    if (fabs(_width - _height) >= std::numeric_limits<double>::epsilon())
    {
//        qDebug() << "width (" << width <<  ") != height (" << height << ")";
        return;
    }

    this->box_count = box_count;
    this->box_size = _width / box_count;



    // Horizontal
    for (auto i = 0; i <= box_count; i++)
    {
        QGraphicsLineItem *line = new QGraphicsLineItem(x, y, x + _width, y);
        QPen pen;
        pen.setColor(Qt::black);
        line->setPen(pen);
//        scene->addLine(x, y, x + _width, y);
        scene->addItem(line);
        y += box_size;
    }

    y = 0.0;

    // Vertical
    for (auto i = 0; i <= box_count; i++)
    {
        scene->addLine(x, y, x, y + _height);
        x += box_size;
    }
}

void mapview::drawWall(const QPointF &endPoint)
{
    auto x = endPoint.x() - margin;
    auto y = endPoint.y() - margin;

    x = static_cast<int>(x / box_size) * box_size;
    y = static_cast<int>(y / box_size) * box_size;

    QGraphicsRectItem* rect = new QGraphicsRectItem(x, y, this->box_size, this->box_size);
    rect->setBrush(QBrush(Qt::red));
    rect->setPen(QPen());
    scene->addItem(rect);
}

void mapview::removeWall(const QPointF &point)
{
    auto x = point.x() - margin;
    auto y = point.y() - margin;

    x = static_cast<int>(x / box_size) * box_size;
    y = static_cast<int>(y / box_size) * box_size;

    QGraphicsRectItem* rect = new QGraphicsRectItem(x, y, this->box_size, this->box_size);
    rect->setBrush(QBrush(Qt::white));
    rect->setPen(QPen());
    scene->addItem(rect);
}

如您在顶部看到的,我设置了抗锯齿功能。问题是,当我设置它时,它以某种方式改变了我的图形的视图:

这是未修改的网格的视图:

enter image description here

现在为1x1框(从0索引)起,我单击了它(使其变成红色),然后双击了它(使其恢复到原始状态)。如您所见,问题是此框的边框有点粗:

enter image description here

有趣的是,它在此视图上仅较厚。如果将其放大,则其边框与其他框的边框没有区别。另一件事是,如果我设置Qt::HighQualityAntialiasing->边界是相同的,但是缩放变慢了,我猜想它必须做更多的繁重的计算或类似的事情。

所以我在这里的问题是:有没有办法使抗锯齿不改变边框的厚度? (我的意思是,我知道它并没有真正改变它,但是您绝对可以在这里看到不同)

0 个答案:

没有答案