单个QGraphicsScene的多个QGraphicsView

时间:2013-05-09 04:21:49

标签: qt4 qgraphicsview qgraphicsscene

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

我需要在各个视图中显示整个场景的特定部分。

为此,我想创建多个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);

2 个答案:

答案 0 :(得分:4)

您正在使用错误的函数,您正在使用setGeometry来告诉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;
}

答案 1 :(得分:1)

QGraphicsScene具有渲染API,您可以使用它来渲染QGraphicsScene的某些部分。你可以通过QWidget渲染它。

void QGraphicsScene :: render(QPainter * painter,const QRectF&amp; target = QRectF(),const QRectF&amp; source = QRectF(),Qt :: AspectRatioMode aspectRatioMode = Qt :: KeepAspectRatio)

类似于以下内容,我测试下面的代码工作正常。

#include "mygraphicsview.h"

#include <QGraphicsScene>
#include <QPixmap>
#include <QGraphicsView>
#include <QPen>
#include <QBrush>

MyGraphicsView::MyGraphicsView(QWidget *parent) :
    QWidget(parent)
{
    setGeometry(QRect(100,100,300,300));

    scene = new QGraphicsScene(QRect(0,0,600,600));
    scene->addRect(20,20,100,100,QPen(),QBrush(Qt::black));
    scene->addRect(10,150,100,100,QPen(),QBrush(Qt::red));
}

void MyGraphicsView::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    scene->render(&painter,QRect(0,0,300,300),QRect(10,10,200,200));
}


#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QWidget>

class QGraphicsScene;

class MyGraphicsView : public QWidget
{
    Q_OBJECT
public:
    MyGraphicsView(QWidget *parent = 0);

    void paintEvent(QPaintEvent *event);

signals:

public slots:

private:
    QGraphicsScene* scene;

};

#endif // MYGRAPHICSVIEW_H