C++ QGraphicsView FitView方法:调整问题大小

C++ QGraphicsView FitView方法:调整问题大小,c++,qt,qt5,qgraphicsview,qgraphicsscene,C++,Qt,Qt5,Qgraphicsview,Qgraphicsscene,我的Qt应用程序中有一个自定义的QGraphicscene(我的类称为MainScene)。此场景包含一定数量的Rect项,这些项就像放置在网格中一样 看图№一, 此外,我还可以动态更改这个矩形网格的大小 图画№二, 此外,我希望这个网格适合大小,所以每次我按下Resize按钮(),场景应该适合大小,如图中所示。我使用以下代码实现它: void MainWindow::on_resizeButton_clicked() { int h = ui->heightSpinBox-&

我的Qt应用程序中有一个自定义的QGraphicscene(我的类称为MainScene)。此场景包含一定数量的Rect项,这些项就像放置在网格中一样

看图№一,

此外,我还可以动态更改这个矩形网格的大小

图画№二,

此外,我希望这个网格适合大小,所以每次我按下Resize按钮(),场景应该适合大小,如图中所示。我使用以下代码实现它:

void MainWindow::on_resizeButton_clicked()
{
    int h = ui->heightSpinBox->value(); //height of the grid
    int w = ui->widthSpinBox->value(); //width of the grid
    scene->resize(h, w); //adding required amount of rects

    ui->graphicsView->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
    ui->graphicsView->centerOn(0, 0);
}
问题是:当我选择高度和宽度时,如果新高度大于当前高度,新宽度大于当前宽度(例如,当前栅格为20x20,我将其调整为30x30),它工作正常,但当我选择小于当前大小的高度和宽度时(例如,当前栅格为30x30,我将其调整为20x20)这不是我想要的

图画№三,

你能告诉我为什么会这样吗?有没有办法解决这个问题

UPD: 以下是我创建网格的方式:

void MainScene::resize(int rows, int cols)
{
    clearScene(rows, cols);
    populateScene(rows, cols);
}

void MainScene::clearScene(int rows, int cols)
{
    if(rows < roomHeight)
    {
        for(int i = rows; i < roomHeight; ++i)
        {
            for(int j = 0; j < roomWidth; ++j)
            {
                removeItem(room[i][j]);
                delete room[i][j];
            }
        }
        room.resize(rows);
        roomHeight = rows;
    }

    if(cols < roomWidth)
    {
        for(int i = 0; i < roomHeight; ++i)
        {
            for(int j = cols; j < roomWidth; ++j)
            {
                removeItem(room[i][j]);
                delete room[i][j];
            }
            room[i].resize(cols);
        }
        roomWidth = cols;
    }
}

void MainScene::populateScene(int rows, int cols)
{
    if(rows > roomHeight)
    {
        room.resize(rows);
        for(int i = roomHeight; i < rows; ++i)
        {
            room[i].resize(roomWidth);
            for(int j = 0; j < roomWidth; ++j)
            {
                    room[i][j] = new GraphicsCell();
                    room[i][j]->setPos(j * 30, i * 30);
                    addItem(room[i][j]);
            }
        }
        roomHeight = rows;
    }

    if(cols > roomWidth)
    {
        for(int i = 0; i < roomHeight; ++i)
        {
            room[i].resize(cols);
            for(int j = roomWidth; j < cols; ++j)
            {
                room[i][j] = new GraphicsCell();
                room[i][j]->setPos(j * 30, i * 30);
                addItem(room[i][j]);
            }
        }
        roomWidth = cols;
    }
}
void主场景::调整大小(int行、int列)
{
(行、列);
流行期(行、列);
}
void mainsecene::clearsene(int行,int列)
{
如果(行数<房间高度)
{
对于(int i=行;i<房间高度;++i)
{
对于(int j=0;j房间高度)
{
调整房间大小(行);
对于(int i=房间高度;i<行数;++i)
{
房间[i]。调整大小(房间宽度);
对于(int j=0;jsetPos(j*30,i*30);
addItem(房间[i][j]);
}
}
房间高度=行数;
}
如果(cols>roomWidth)
{
对于(int i=0;isetPos(j*30,i*30);
addItem(房间[i][j]);
}
}
roomWidth=cols;
}
}

Graphicsell是我的自定义类,它派生自
QObject
QGraphicsItem
room
是GraphicsCell对象的矢量。

添加项目时,如果项目不在
ScenInsert
范围内,则
ScenInsert
会增加,这就是添加
30x30
时发生的情况,但当通过
20x20
时,它不会减小,因此场景仍然很大,因此您可以观看
QScrollBar
s,因此在这种情况下,解决方案是将场景大小更新为
itemsBoundingRect()

void主窗口::在重新调整按钮时单击()
{
int h=ui->heightSpinBox->value();//网格的高度
int w=ui->widthSpinBox->value();//网格的宽度
场景->调整大小(h,w);//添加所需数量的矩形
ui->graphicsView->fitInView(场景->itemsBoundingRect(),Qt::KeepAspectRatio);

场景->设置场景竖立(场景->项目边界矩形());//显示如何创建grids@eyllanesc请看UPDMainScene是QGraphicscene吗?@eyllanesc是的。对不起,我没有匹配它。我会更正它
void MainWindow::on_resizeButton_clicked()
{
    int h = ui->heightSpinBox->value(); //height of the grid
    int w = ui->widthSpinBox->value(); //width of the grid
    scene->resize(h, w); //adding required amount of rects

    ui->graphicsView->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
    scene->setSceneRect(scene->itemsBoundingRect()); // <---
}