运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/18d2e6cbf3bf51863df62f26c24ae50b.png)
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QGraphicsView *gv;
QGraphicsScene *scene;
};
#endif
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
scene = new QGraphicsScene(this);
scene->addLine(0, 0, 100, 100, QPen(Qt::green));
gv = new QGraphicsView(scene, this);
gv->setBackgroundBrush(Qt::black);
setCentralWidget(gv);
}
MainWindow::~MainWindow()
{
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}