gpt4 book ai didi

qt4 - 单个 QGraphicsScene 的多个 QGraphicsView

转载 作者:行者123 更新时间:2023-12-04 15:12:43 29 4
gpt4 key购买 nike

我有一个 QGraphicsScene我添加了一些 QGraphicsItem 的实例.

我需要在单个 View 中显示整个场景的特定部分。

为此,我想创建 QGraphicsView 的多个实例。每个都显示我的 QGraphicsScene 的特定部分(不是类似的部分)。

怎么做到呢?

QGraphicsScene mcpGraphicsScene = new QGraphicsScene(this);

QGraphicsRectItem * mcpGraphicsRect = mcpGraphicsScene->addRect(5,5,200,200);

QGraphicsLineItem * mcpGraphicsLine = mcpGraphicsScene->addLine(500,500,300,300);


QGraphicsView * mcpGraphicsView1 = new QGraphicsView(this);
mcpGraphicsView1->setScene(mcpGraphicsScene);
mcpGraphicsView1->setGeometry(260,20,311,500);

QGraphicsView * mcpGraphicsView2 = new QGraphicsView(this);
mcpGraphicsView2->setScene(mcpGraphicsScene);
mcpGraphicsView2->setGeometry(260,520,311,1061);

最佳答案

您使用了错误的函数,您使用的是 setGeometry,它告诉 View 应该相对于其父级(即小部件,而不是场景)放置它的位置。要定义 View 负责显示的场景区域,您需要调用 use setSceneRect

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QLayout>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QHBoxLayout* myLayout = new QHBoxLayout(this);
QGraphicsScene* mcpGraphicsScene = new QGraphicsScene(this);

mcpGraphicsScene->addRect(5,5,200,200);
mcpGraphicsScene->addLine(500,500,300,300);

QGraphicsView * mcpGraphicsView1 = new QGraphicsView(mcpGraphicsScene, this);
mcpGraphicsView1->setSceneRect(0,0,150,150);

QGraphicsView * mcpGraphicsView2 = new QGraphicsView(mcpGraphicsScene, this);
mcpGraphicsView2->setSceneRect(0,150,600,600);

myLayout->addWidget(mcpGraphicsView1);
myLayout->addWidget(mcpGraphicsView2);
QWidget *window = new QWidget();
window->setLayout(myLayout);
setCentralWidget(window);
}

MainWindow::~MainWindow()
{
delete ui;
}

关于qt4 - 单个 QGraphicsScene 的多个 QGraphicsView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16454308/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com