开发者

How can I programmatically add a QGraphicsView inside a QMainWindow?

开发者 https://www.devze.com 2023-01-30 03:22 出处:网络
I would like to add a QGraphicsView(a sub class of it called Menu) inside a QMainWindow programmatically (using code).

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消