QGraphicsView、QGraphicsScene和QGraphicsItem图形视图框架(二)疑难杂症

疑难杂症1

1.问题:

设置场景的背景图片时,采用setBackgroundBrush()方法和重写drawBackground()函数得到的结果很不一样,而且通过setSceneRect设置场景原点位置之后得到的结果也有很大区别。

如下图

第一个和第三个中重写了QGraphicsScene的drawBackground()函数,区别在于第一个的场景原点在左上角,第三个的原点在中心。

第二个和第四个直接用的QGraphicsScene通过setBackgroundBrush()函数设置背景图片,区别在于第二个的场景原点在左上角,第四个的原点在中心。

class GraphicsScene : public QGraphicsScene
{
    Q_OBJECT
public:
    explicit GraphicsScene(QObject *parent = nullptr)
        :QGraphicsScene(parent)
    {

    }

protected:
    void drawBackground(QPainter *painter, const QRectF &rect)
    {
        Q_UNUSED(rect)
        painter->drawPixmap(sceneRect().toRect(),QPixmap("/home/li/1.png"));
    }
};

便于查看原点位置,在原点绘制了十字架 

    // 绘制十字路径
    {
        // 创建 QPainterPath 对象
        QPainterPath path;
        path.moveTo(-50, 0);
        path.lineTo(50, 0);
        path.moveTo(0, -50);
        path.lineTo(0, 50);

        // 创建 QGraphicsPathItem 对象,并设置路径
        QGraphicsPathItem *pathItem = new QGraphicsPathItem(path);
        pathItem->setPen(QPen(Qt::black, 1));
        pathItem->setPos(0,0);
        scene1->addItem(pathItem);

        QGraphicsRectItem *rect = new QGraphicsRectItem(-1,-1,2,2);
        rect->setBrush(Qt::red);
        rect->setPen(QPen(Qt::red));
        rect->setPos(0,0);
        scene1->addItem(rect);
    }

四个不同的显示代码如下:

//1.
GraphicsScene *scene1 = new GraphicsScene;
scene1->setSceneRect(0,0,400,400);
ui->graphicsView->setScene(scene1);

//2.
QGraphicsScene *scene2 = new QGraphicsScene;
scene2->setSceneRect(0,0,400,400);
scene2->setBackgroundBrush(QPixmap("/home/li/1.png"));
ui->graphicsView_2->setScene(scene2);

//3.
GraphicsScene *scene3 = new GraphicsScene;
scene3->setSceneRect(-200,-200,400,400);
ui->graphicsView_3->setScene(scene3);

//4.
QGraphicsScene *scene4 = new QGraphicsScene;
scene4->setSceneRect(-200,-200,400,400);
scene4->setBackgroundBrush(QPixmap("/home/li/1.png"));
ui->graphicsView_4->setScene(scene4);

这是为什么呢???为什么显示的结果不一样?为什么直接通过setBackgroundBrush设置的背景,在超出场景边界的地方以场景的背景填充的?

2.解答

找了一下drawBackground()和setBackgroundBrush()的源码实现,如下:

void QGraphicsScene::setBackgroundBrush(const QBrush &brush)
{
    Q_D(QGraphicsScene);
    d->backgroundBrush = brush;
    for (QGraphicsView *view : std::as_const(d->views)) {
        view->resetCachedContent();
        view->viewport()->update();
    }
    update();
}
void QGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    Q_D(QGraphicsScene);

    if (d->backgroundBrush.style() != Qt::NoBrush) {
        if (d->painterStateProtection)
            painter->save();
        painter->setBrushOrigin(0, 0);
        painter->fillRect(rect, backgroundBrush());
        if (d->painterStateProtection)
            painter->restore();
    }
}

通过源码可以通过setBackgroundBrush设置背景本质上还是drawBackground函数绘制的背景。

[virtual protected] void QGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
Draws the background of the scene using painter, before any items and the foreground are drawn. Reimplement this function to provide a custom background for the scene.
All painting is done in scene coordinates. The rect parameter is the exposed rectangle.
If all you want is to define a color, texture, or gradient for the background, you can call setBackgroundBrush() instead. 

drawBackground函数的官方解释中,参数rect是暴露的矩形。

图片中的第一个和第三个只显示了sceneRect()矩形内的背景是因为在重写的drawBackground函数中,painter->drawPixmap(sceneRect().toRect(),QPixmap("/home/li/1.png"));只在设置的场景矩形框内绘制背景。源码中painter->fillRect(rect, backgroundBrush());在给定的暴露的场景内绘制。

void QPainter::setBrushOrigin(const QPointF &position)

Sets the brush origin to position.

The brush origin specifies the (0, 0) coordinate of the painter's brush.

setBrushOrigin函数的官方解释,将画笔原点设置为给定位置。

图片中的第二个和第四个的差异是因为在源码中写到painter->setBrushOrigin(0, 0);将画笔原点设置为(0,0)点,所以绘制背景图片时是从(0,0)点作为起点的。

3.综合

如果想要以不规则的图片作为背景图片,需要重写QGraphicsScene,重写drawBackground()函数。

class GraphicsScene : public QGraphicsScene
{
    Q_OBJECT
public:
    explicit GraphicsScene(QObject *parent = nullptr)
        :QGraphicsScene(parent)
    {

    }

protected:
    void drawBackground(QPainter *painter, const QRectF &rect)
    {
        painter->drawPixmap(rect.toRect(),QPixmap("/home/li/1.png"));
    }
};

疑难杂症2

1.问题

默认情况下,当在 QGraphicsView 中设置一个 QGraphicsScene 实例时,视图将自动调整滚动区域以适应场景的大小,确保场景的中心在视图中居中显示。这一机制确保了无论场景大小如何变化,视图始终能保持场景中心点的可视性。

但是!!!下面出现了特殊情况。

由于工作中用到在QTabWidget控件中放两个完全一样的QGraphicsView并使用布局,然后在场景中心放十字架,发现总有一个view的十字架不在中心。

放到同一个tab里面用布局都不在中间

在tab中不用布局,直接设置view的大小就是正常的。

还尝试了除QTabWidget之外可以放多个窗口的控件,是没问题。

2.解答

通过上面的对比,可以发现是QTabWidget里面的布局对QGraphicsView中的滚动条位置产生了影响,使场景中心和视图的视口中心不能对应起来。(但是是为什么会有这个影响就不太清楚了)

 消除这个的影响只能主动设置一下QGraphicsView的滚动条位置,设置滚动条的位置在中心。

 ui->graphicsView->horizontalScrollBar()->setValue(-ui->graphicsView->viewport()->rect().width()/2);
 ui->graphicsView->verticalScrollBar()->setValue(-ui->graphicsView->viewport()->rect().height()/2);

注意:在设置时,需要在窗口完全显示之后设置,因为要获取QGraphicsView的宽度和高度,在未完全显示之前,拿到的宽高都是默认的原始宽高;只要显示出来之后,才可以拿到准确的宽高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值