Multiple QGraphicsView for one QGraphicsScene

I have one QGraphicsScene to which I have added several instances of QGraphicsItem .

I need to display a specific section of my entire scene in separate views.

To do this, I want to create several QGraphicsView instances, each of which displays a specific section of my QGraphicsScene (not a similar part).

How can I do that?

 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); 
+6
source share
2 answers

You are using the wrong function, you are using setGeometry, which tells View where it should be placed relative to its parent (which is a widget, not a scene). To determine the area of ​​the scene to which the view responds, you must call the setSceneRect function

 #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; } 
+4
source

QGraphicsScene has a rendering API that you can use to render a specific part of QGraphicsScene. You can display it on top of QWidget.

void QGraphicsScene :: render (QPainter * artist, const QRectF and target = QRectF (), const QRectF and source = QRectF (), Qt :: AspectRatioMode aspectRatioMode = Qt :: KeepAspectRatio)

something like the following, i tested the following code is working fine.

 #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 
+1
source

Source: https://habr.com/ru/post/944598/


All Articles