QGraphicsScene 在 QGraphicsView 中奇怪地缩放 [英] QGraphicsScene scaled weirdly in QGraphicsView

查看:126
本文介绍了QGraphicsScene 在 QGraphicsView 中奇怪地缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QGraphicsViewQGraphicsScene 来创建一个 Tic Tac Toe 克隆.我向我的场景中添加了一些 QGraphicsLineItem 并覆盖包含视图的小部件的 resizeEvent 方法,以便在调整窗口大小时,缩放视图及其内容适当地.这工作正常,除了我第一次运行程序:

I'm messing around with QGraphicsView and QGraphicsScene to create a Tic Tac Toe clone. I add some QGraphicsLineItems to my scene and override the resizeEvent method of the Widget that contains the view, so that when the window is resized, the view and its contents are scaled appropriately. This works fine, except for the first time that I run the program:

一旦我按任意量调整窗口大小,场景就会正确缩放:

Once I resize the window by any amount, the scene is scaled correctly:

代码如下:

ma​​in.cpp:

#include <QtGui>

#include "TestApp.h"

int main(int argv, char **args)
{
    QApplication app(argv, args);

    TestApp window;
    window.show();

    return app.exec();
}

TestApp.h:

#ifndef TEST_APP_H
#define TEST_APP_H

#include <QtGui>

class TestApp : public QMainWindow
{
    Q_OBJECT
public:
    TestApp();
protected:
    void resizeEvent(QResizeEvent* event);

    QGraphicsView* view;
    QGraphicsScene* scene;
};

#endif

TestApp.cpp:

#include "TestApp.h"

TestApp::TestApp()
: view(new QGraphicsView(this))
, scene(new QGraphicsScene(this))
{
    resize(220, 220);
    scene->setSceneRect(0, 0, 200, 200);

    const int BOARD_WIDTH = 3;
    const int BOARD_HEIGHT = 3;
    const QPoint SQUARE_SIZE = QPoint(66, 66);

    const int LINE_WIDTH = 10;
    const int HALF_LINE_WIDTH = LINE_WIDTH / 2;
    QBrush lineBrush = QBrush(Qt::black);
    QPen linePen = QPen(lineBrush, LINE_WIDTH);

    for(int x = 1; x < BOARD_WIDTH; ++x)
    {
        int x1 = x * SQUARE_SIZE.x();
        scene->addLine(x1, HALF_LINE_WIDTH, x1, scene->height() - HALF_LINE_WIDTH, linePen);
    }
    for(int y = 1; y < BOARD_HEIGHT; ++y)
    {
        int y1 = y * SQUARE_SIZE.y();
        scene->addLine(HALF_LINE_WIDTH, y1, scene->width() - HALF_LINE_WIDTH, y1, linePen);
    }

    view->setScene(scene);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->show();
    view->installEventFilter(this);

    setCentralWidget(view);
}

void TestApp::resizeEvent(QResizeEvent* event)
{
    view->fitInView(0, 0, scene->width(), scene->height());
    QWidget::resizeEvent(event);
}

我尝试在 TestApp 的构造函数的末尾添加对 fitInView 的调用,但它似乎没有做任何事情 - resizeEvent 似乎在程序执行开始时被调用一次.

I've tried adding a call to fitInView at the end of TestApp's constructor, but it doesn't seem to do anything - resizeEvent seems to be called once at the start of the program's execution anyway.

干杯.

推荐答案

在 showEvent 中处理视图适配:

Handle the view fit also inside the showEvent:

void TestApp::showEvent ( QShowEvent * event )
{
    view->fitInView(0, 0, scene->width(), scene->height());
    QWidget::showEvent(event);
}

这篇关于QGraphicsScene 在 QGraphicsView 中奇怪地缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆