I would like to add a QGraphicsView(a sub class of it called Menu) inside a QMainWindow programmatically (using code).
In fact I already do that but the problem is the embedded QGraphicsView is not showed.
Here is the code I used inside QMainWindow::init()
menuView=new Menu(this);
menuView->show();
I already create the scene and insert ite开发者_Python百科ms inside the Menu class.
What's wrong?
try
menuView=new Menu(this);
menuView->show();
QLayout* layout=new QVBoxLayout();
layout->addWidget(menuView);
this->setLayout(layout);
if you are not using layouts, or
menuView=new Menu(this);
menuView->show();
QLayout* layout=this->layout();
layout->addWidget(menuView);
this->setLayout(layout);
if your form already have a layout.
When using QMainWindow, use setCentralWidget:
int main( int argc, char** argv ) {
QApplication app( argc, argv );
...
QMainWindow mw;
Menu menu;
mw.setCentralWidget( &menu );
mw.show();
return app.exec();
}
精彩评论