简体   繁体   中英

addWidget doesn't work

I want to add a widget to the QGraphicsScene by clicking a button but it doesn't work. Anybody any idea what I'm doing wrong ? I setup the QGraphicsScene like this:

ui->setupUi(this);

mScene = new QGraphicsScene(this);
mNodeView = new QNodeView(ui->tabWidget);
ui->NodeGraphicsView->addWidget(mNodeView);

mNodeView->setScene(mScene);
mNodeView->show();

Add the widget to the QGraphicsScene

void MainWindow::on_actionTextNode_triggered()
{
    QNodeWidget *_nodeWidget = new QNodeWidget(mNodeView);
    mScene->addWidget(_nodeWidget);

    mNodeView->show();
}

It looks like you try set parent to _nodeWidget , try to do it without parent:

QNodeWidget *_nodeWidget = new QNodeWidget;

Is it work?

When you add a widget to a scene, it's added as a QProxyWidget at position (0,0).

If you can't see the widget, it's possible that it's just not in view. First, check that the call to addWidget returns a valid pointer and if so, focus the view on the widget with a call to fitInWindow: -

QNodeWidget *_nodeWidget = new QNodeWidget(mNodeView);
QGraphicsProxyWidget* pProxy = mScene->addWidget(_nodeWidget);

mNodeView->fitInWindow(pProxy);

If you didn't get a valid pointer to the proxy widget, then there's a problem with the QNodeWidget class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM