当前位置:网站首页>图形视图框架
图形视图框架
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函数实现移动,需要绑定定时器
碰撞检测
边栏推荐
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- [QT] QT multithreading development - four methods to realize multithreading design
- Simpleitk use - 4 Strange question
- C语言,实现三子棋小游戏
- NC24325 [USACO 2012 Mar S]Flowerpot
- Service visibility and observability
- php优化foreach中的sql查询
- Server response status code
- Bridge emqx cloud data to AWS IOT through the public network
- Introduction to database system Chapter 1 short answer questions - how was the final exam?
猜你喜欢
Socket socket c/s end process
NC24325 [USACO 2012 Mar S]Flowerpot
对象与对象变量
Ransack组合条件搜索实现
Phpcms realizes the direct Alipay payment function of orders
20220702 how do programmers build knowledge systems?
20220702-程序员如何构建知识体系?
Pointer and string
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Simpleitk use - 3 Common operations
随机推荐
Methods of adding styles to native JS
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Infrastructure is code: a change is coming
ServiceMesh主要解决的三大痛点
C language, to achieve three chess games
[QT] QT multithreading development - reentrancy and thread safety
Hanoi Tower problem
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
Leetcode theme [array] -169- most elements
Necessary browser plug-ins for network security engineers
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
[shutter] shutter application theme (themedata | dynamic modification theme)
Dynamic memory allocation (malloc calloc realloc free)
U++ 学习笔记 堆
#include errors detected. Please update your includePath.
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
使用 EMQX Cloud 实现物联网设备一机一密验证
ArrayList analysis 2: pits in ITR, listiterator, and sublist