当前位置:网站首页>图形视图框架
图形视图框架
2022-07-02 22:06:00 【Htht111】
思路
新建场景、图元、和视图。
图元添加到场景,场景与视图关联,视图显示。
初始例子
头文件
#include <QGraphicsScene> //场景:添加图元 void addItem(QGraphicsItem *item)
#include <QGraphicsView> //视图;显示场景
#include <QGraphicsItem> //图元
main.cpp
QGraphicsScene *scene = new QGraphicsScene; //场景
QGraphicsRectItem *item = new QGraphicsRectItem(100,100,50,50); //矩形项
scene->addItem(item); //项添加到场景
QGraphicsView *view = new QGraphicsView; //视图
view->setScene(scene); //视图关联场景
view->resize(500,500);
view->show(); //显示视图
具体操作
新建一个图元类MyItem继承QGraphicsItem,可以实现自定义的图元项
注意需要重写两个虚函数boundingRect()和paint(),不然编译会有错误
myitem.h
#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsItem>
class MyItem : public QGraphicsItem
{
public:
MyItem();
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
};
#endif // MYITEM_H
myitem.cpp
#include "myitem.h"
#include <QPainter>
MyItem::MyItem()
{
}
QRectF MyItem::boundingRect() const
{
qreal penWidth = 1;//笔宽
return QRectF(0 - penWidth/2,0-penWidth/2,20+penWidth,20+penWidth);//返回矩形边框(坐标、宽高)
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//标明该参数没有使用
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setBrush(Qt::green);//用绿色的 笔刷
painter->drawEllipse(0,0,50,50);//画圆
}
效果1:一个绿色的圆形::
main.cpp
#include "widget.h"
#include <QApplication>
#include "animation.h"
#include <QDebug>
#include "myitem.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Widget w;
// w.show();
QGraphicsScene *scene = new QGraphicsScene; //场景
// QGraphicsRectItem *item = new QGraphicsRectItem(100,100,50,50); //矩形项
MyItem *item=new MyItem;//新建图元类对象
scene->addItem(item); //项添加到场景
QGraphicsView *view = new QGraphicsView; //视图
view->setScene(scene); //视图关联场景
view->resize(500,500);
view->show(); //显示视图
return a.exec();
}
效果2:两个绿色的圆形
操作:再定义一个图元类对象,记得修改第二个图元位置(防止与第一个重合)
main.cpp
#include "widget.h"
#include <QApplication>
#include "animation.h"
#include <QDebug>
#include "myitem.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene *scene = new QGraphicsScene; //场景
// QGraphicsRectItem *item = new QGraphicsRectItem(100,100,50,50); //矩形项
MyItem *item=new MyItem;
MyItem *item1=new MyItem;
scene->addItem(item); //项添加到场景
scene->addItem(item1);
item1->setPos(66,66);//设置第二个图元位置
QGraphicsView *view = new QGraphicsView; //视图
view->setScene(scene); //视图关联场景
view->resize(500,500);
view->show(); //显示视图
return a.exec();
}
图元移动
定义advance函数实现移动,需要绑定定时器
碰撞检测
边栏推荐
- Try to get property'num for PHP database data reading_ rows' of non-object?
- Reading experience of just because
- 服务器响应状态码
- Leetcode circular linked list (fast and slow pointer) code line by line interpretation
- Struct, bit segment, enumeration, union
- Market Research - current situation and future development trend of marine clutch Market
- phpcms实现订单直接支付宝支付功能
- I admire that someone explained such an obscure subject as advanced mathematics so easily
- Market Research - current situation and future development trend of cell-based seafood market
- Market Research - current market situation and future development trend of aircraft front wheel steering system
猜你喜欢
Socket套接字C/S端流程
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
任务和特权级保护
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Socket socket c/s end process
sql service 截取字符串
20220702 how do programmers build knowledge systems?
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
From "bronze" to "King", there are three secrets of enterprise digitalization
随机推荐
影视随摘
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
Market Research - current market situation and future development trend of aircraft audio control panel system
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Oracle PL / SQL programming
情感计算与理解研究发展概述
[shutter] shutter resource file use (import resource pictures | use image resources)
[C question set] of V
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Commodity information management system (C language document version)
Market Research - current market situation and future development trend of marine wet exhaust hose
开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
Web侧防御指南
Evolution of messaging and streaming systems under the native tide of open source cloud
建立自己的网站(22)
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
Struct, bit segment, enumeration, union
数学建模——图与网络模型及方法(一)
Web side defense Guide
Market Research - current market situation and future development trend of aircraft front wheel steering system