Search code examples
qwidgetqgraphicsviewqgraphicsscene

Qt GraphicsScene on Widget


How to add Graphics Scene/Graphics View to Widget?

Code here


Solution

  • This is my Solution:

    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QGraphicsView>
    #include <QGraphicsTextItem>
    #include <QGraphicsScene>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QWidget *window = new QWidget;
        window->resize(300, 200);
        QVBoxLayout *layout = new QVBoxLayout(window);
        QGraphicsScene scene;
        QGraphicsView *view = new QGraphicsView(&scene);
        QGraphicsTextItem *text =  scene.addText("Hello World");
        layout->addWidget(view);
        window->show();
        return a.exec();
    }
    

    Output:

    enter image description here