Widget上的Qt GraphicsScene

时间:2016-12-29 21:28:17

标签: qwidget qgraphicsview qgraphicsscene

如何将图形场景/图形视图添加到窗口小部件?

Code here

2 个答案:

答案 0 :(得分:0)

这是我的解决方案:

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

输出:

enter image description here

答案 1 :(得分:0)

非常感谢您的回答。这也有效。

#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();
}
相关问题