小部件上的 Qgraphicsscene

Qt GraphicsScene on Widget

如何将图形 Scene/Graphics 视图添加到小部件?

Code here

这是我的解决方案:

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

输出:

非常感谢您的回答。这也行。

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