Qt GraphicsScene on Widget

1.1k views Asked by At

How to add Graphics Scene/Graphics View to Widget?

Code here

2

There are 2 answers

0
eyllanesc On BEST ANSWER

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

0
AudioBubble On

Thank you so much for your answer. This also it works.

#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 = new QGraphicsScene(window);
    QGraphicsView *view = new QGraphicsView(scene);
    QGraphicsTextItem *text =  scene->addText("Hello World");
    layout->addWidget(view);
    window->show();
    return a.exec();
}