【问题标题】:Qt GraphicsScene on Widget小部件上的 Qt GraphicsScene
【发布时间】:2016-12-29 21:28:17
【问题描述】:

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

Code here

【问题讨论】:

  • 请显示您的代码

标签: qwidget qgraphicsview qgraphicsscene


【解决方案1】:

这是我的解决方案:

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

输出:

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多