如何在mainwindow.cpp中将场景添加到视图中

How to add a scene to a view in mainwindow.cpp?

本文关键字:添加 视图 mainwindow cpp      更新时间:2023-10-16

进行搜索这就是我在"main.cpp"中找到并放置的内容:

QGraphicsScene scene;
QGraphicsView view(&scene);

但我需要类似以下内容,并放置在"mainwindow.cpp"中:

QGraphicsScene scene;
QGraphicsView *view = new QGraphicsView();
view->addScene(&scene); //need something like this

这主要起作用,并显示"黄色"背景。但是当我在mainwindow.cpp…中使用setScene进行更改时,黄色背景不会出现。

main.cpp

 QGraphicsScene scene;
 QGraphicsView view(&scene);
 view.setRenderHint(QPainter::Antialiasing);
 view.setBackgroundBrush(Qt::yellow);
 view.setCacheMode(QGraphicsView::CacheBackground);
 view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
 view.setDragMode(QGraphicsView::ScrollHandDrag);
 view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
 view.resize(1000, 800);
 view.show();

mainwindow.cpp:没有黄色背景

 QGraphicsScene scene;
 QGraphicsView *view = new QGraphicsView();
 view->setScene(&scene);
 view->setRenderHint(QPainter::Antialiasing);
 view->setBackgroundBrush(Qt::yellow);
 view->setCacheMode(QGraphicsView::CacheBackground);
 view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
 view->setDragMode(QGraphicsView::ScrollHandDrag);
 view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
 view->resize(1000, 800);
 view->show();

您想要的是'setScene',而不是'addScene'。由于一次只能有一个场景设置到一个视图中,因此"set"是函数名称的正确单词,这意味着它将替换以前存在的任何场景add'意味着即使添加了新场景,旧场景仍然存在,而QGraphicsView的情况并非如此。

QGraphicsScene scene;
QGraphicsView *view = new QGraphicsView();
view->setScene(&scene); //<--- The function name you want.

当然,您的QGraphicsView实际上需要设置为主窗口。如果你想让它填满整个主窗口,请使用:

this->setCentralWidget(view); //Assuming 'this' is the QMainWindow widget.

正如@Merlin069建议的那样。

如果你不想让它填满整个窗口,但也想要其他东西,你应该在主窗口小部件中添加一个布局,并在布局中添加视图:

//Create the layout.
QHBoxLayout *horizontalLayout = new QHBoxLayout;
//Add widgets to the layout.
horizontalLayout->addWidget(sidepanelOnTheLeft);
horizontalLayout->addWidget(view);
horizontalLayout->addWidget(sidepanelOnTheRight);
//Set the layout to the widget that owns it.
this->centralWidget()->setLayout(horizontalLayout);

Qt的文档非常好。你绝对应该加书签:

Qt文档:类索引

新答案,因为它在OP更新问题时回答了不同的问题

您的问题是QGraphicsScene scene;是在函数的本地创建的(我认为它是MainWindow构造函数)。

这意味着:

function()
{
   QGraphicsScene scene; //Creates the scene.
   QGraphicsView *view = new QGraphicsView(); //Creates the view
   view->setScene(&scene); //Adds the scene to the view, but *the view does not take ownership*
   //...other stuff...
   view->show(); //Show the view.
} //<--- The scene gets destroyed because it was local, and when being destroyed, removes itself from the view.

相反,QGraphicsScene应该是动态分配的,并由main的"this"指针(或main的成员变量)"owned":

MainWindow::MainWindow()
{
   QGraphicsScene *scene = new QGraphicsScene(this /* Gives ownership to MainWindow */);
   QGraphicsView *view = new QGraphicsView(this /* Gives ownership to MainWindow */);
   view->setScene(scene);
   view->setWhateverSettingsYouWant();
   scene->setWhateverSettingsYouWant();
   //Tell the MainWindow that you want the view to be *inside* the MainWindow.
   //Also gives ownership to the MainWindow (again, but it won't hurt anything).
   this->setCentralWidget(view);
}

QWidgets(以及QWidgets继承的QObjects)具有父层次结构->子层次结构。当一个父母被摧毁时,它也会释放每个孩子这很重要。如果你只是"新"的东西,它们永远不会被删除!因此,它们必须由父级"拥有",这样当父级本身被销毁时,父级就可以删除它们。

除了@JaminGrey的答案外,您还可以调用QMainWindow函数setCentralWidget,传递QGraphicsView对象将其添加到QMainWindow。

如果您没有使用QMainWindow,请调用QGraphicsView函数show()。请注意,您还需要将对象添加到QGraphicsScene中,以便查看某些内容。

在将QGraphicsScene添加到QGraphicsView之前,请确保对其进行了动态分配,因为该视图将指针指向场景。