1. 定义一个带界面的类QFormTable,继承QWidget,该类的实现如下
头文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <QWidget> #include "ui_QFormTable.h" class QFormTable : public QWidget { Q_OBJECT public: QFormTable(QWidget *parent = Q_NULLPTR); ~QFormTable(); private: void paintEvent(QPaintEvent *event); private: Ui::QFormTable ui; };
源文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "QFormTable.h" #include <QgraphicsItem> QFormTable::QFormTable(QWidget *parent) : QWidget(parent) { ui.setupUi(this); } QFormTable::~QFormTable() { } void QFormTable::paintEvent(QPaintEvent *event) { QGraphicsScene * scene = new QGraphicsScene(QRect(0, 0, width(), height())); ui.graphicsView->setScene(scene); QGraphicsRectItem * item = new QGraphicsRectItem(QRect(width()/3, height()/3, width()/2, height()/2)); item->setFlags(QGraphicsRectItem::ItemIsSelectable | QGraphicsRectItem::ItemIsFocusable); QPen pen; pen.setWidth(2); item->setPen(pen); scene->addItem(item); }
2. 在界面添加一个控件QGraphicsView,用于显示场景中的内容。
上述源文件重写了paintEvent事件,给graphicsView添加了一个场景scene,然后可以新建各种图形的QGraphicsItem,
把这些item加到场景scene中。可以给item设置颜色,是否可选,是否可设焦点。
最后
以上就是文艺太阳最近收集整理的关于QGraphics View绘图架构实例的全部内容,更多相关QGraphics内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复