如何在mainwindow.cpp中将场景添加到视图中
How to add a scene to a view in mainwindow.cpp?
进行搜索这就是我在"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之前,请确保对其进行了动态分配,因为该视图将指针指向场景。
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 视图中的参数推导失败:take_while
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- 在另一个类视图中添加最多2个图表的正确方法是什么
- MFC MDI 向文档添加第二个视图
- 如何在移动列表视图中添加滚动条?并且用户只能垂直滚动特定区域
- 为 Web 视图添加 UI 栏按钮项,但应用崩溃
- WinRT W8 在C++中创建 Web 视图并将其添加到主视图
- 无法将目录添加到视图中
- 如何在mainwindow.cpp中将场景添加到视图中
- RootViewController 在其委托外部访问时不会添加子视图
- 在Qt creator中只添加可读行到表视图
- 将行动态添加到表视图中