QGraphics View绘图架构实例

1. 定义一个带界面的类QFormTable,继承QWidget,该类的实现如下

头文件

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

源文件

#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设置颜色,是否可选,是否可设焦点。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值