进行搜索这是我找到并放在“main.cpp”中的内容:
QGraphicsScene scene;
QGraphicsView view(&scene);
但是我需要类似下面的内容并放在“mainwindow.cpp”中:
QGraphicsScene scene;
QGraphicsView *view = new QGraphicsView();
view->addScene(&scene); //need something like this
这在main中有效并显示“黄色”背景。但是当我在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();
答案 0 :(得分:2)
你想要' setScene '不是' addScene'。由于一次只能有一个场景设置到一个视图,因此设置'是函数名称的正确单词,暗示它取代之前的任何场景。 '添加'即使添加了新场景,也会暗示旧场景仍然存在,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的文档非常好。你一定要书签:
答案 1 :(得分:2)
(新答案,因为它在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的成员变量):
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
(和QObjects
,QWidgets
继承)具有parent-&gt;子层次结构。当父母被摧毁时,它也会释放每个孩子。 这很重要。如果你只是'新'的东西,它们永远不会被删除!因此,它们必须由父级“拥有”,因此父级可以在父级本身被销毁时删除它们。
答案 2 :(得分:1)
除了@JaminGrey的答案,你可以调用QMainWindow函数setCentralWidget,传递QGraphicsView对象将它添加到QMainWindow。
如果您没有使用QMainWindow,请调用QGraphicsView函数show()。请注意,您还需要向QGraphicsScene添加对象才能看到内容。
确保在添加到QGraphicsView之前动态分配QGraphicsScene,因为视图会获取指向场景的指针。