当前位置:网站首页>图形视图框架
图形视图框架
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函数实现移动,需要绑定定时器
碰撞检测
边栏推荐
- From personal heroes to versatile developers, the era of programmer 3.0 is coming
- Pointer and string
- PHP wechat red packet grabbing algorithm
- 关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Market Research - current market situation and future development trend of high tibial osteotomy plate
- [QT] QT multithreading development - four methods to realize multithreading design
- New feature of go1.18: introduce new netip Network Library
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- Market Research - current situation and future development trend of sickle cell therapy Market
猜你喜欢
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!

Simpleitk use - 3 Common operations

数学建模——图与网络模型及方法(一)

Kubernetes resource object introduction and common commands (4)

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?

基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习

对象与对象变量

UE4 game architecture learning notes

New feature of go1.18: introduce new netip Network Library
随机推荐
Market Research - current situation and future development trend of anti-counterfeiting label market
Service visibility and observability
Build your own website (22)
Bridge emqx cloud data to AWS IOT through the public network
Market Research - current situation and future development trend of herringbone gear Market
Attack and defense world PWN question: Echo
Promise optimized callback hell
使用 EMQX Cloud 实现物联网设备一机一密验证
PHP wechat red packet grabbing algorithm
C language, to achieve three chess games
SimpleITK使用——4. 奇怪的問題
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Sql service intercepts string
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Struct, bit segment, enumeration, union
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
Evolution of messaging and streaming systems under the native tide of open source cloud
New feature of go1.18: introduce new netip Network Library
Market Research - current situation and future development trend of environmental friendly fireworks Market
Necessary browser plug-ins for network security engineers