Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to add qgraphicscene/view to qwidget



  • how do i add qgraphicscene/view to qwidget

    for example, this create a separate window than wdiget.

    scene and view are qgraphicscene and qgraphicview declare in mytest_widget class

    mytest_widget::mytest_widget(QWidget *parent) : QWidget(parent)
    {
    rect = new QGraphicsRectItem();

    scene.addItem(rect);

    view.setScene(scene);
    view.show();
    }


  • Lifetime Qt Champion

    Hi,

    Take a look at the Layout Management chapter in Qt's documentation.



  • thx, i look at it, but it didn't mention qgraphscene. i add QHBoxLayout etc before with button etc, but not sure about qgraphicscene?

    something like this

    mytest_widget::mytest_widget(QWidget *parent) : QWidget(parent)
    {
    mytest_widget_dis *display = new mytest_widget_dis();
    display->view->show();
    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addWidget( display );

    }

    mytest_widget_dis::mytest_widget_dis(QWidget *parent)

    {
    rect = new QGraphicsRectItem();
    rect->setRect(0,0,100,100);
    rect->setBrush(Qt::black);
    pen.setColor(Qt::green);
    rect->setPen(pen);
    scene = new QGraphicsScene(this);
    scene->addItem(rect);

    view=new QGraphicsView(scene);

    }

    thought Scene still show a separated GUI from main widget


  • Lifetime Qt Champion

    You are not setting your layout on the widget.


Log in to reply