当前位置:网站首页>Graphic view frame
Graphic view frame
2022-07-02 22:46:00 【Htht111】
Ideas
New scene 、 Primitives 、 And views .
Elements are added to the scene , The scene is associated with the view , View display .
Initial example
The header file
#include <QGraphicsScene> // scene : Add elements void addItem(QGraphicsItem *item)
#include <QGraphicsView> // View ; Show scene
#include <QGraphicsItem> // Primitives
main.cpp
QGraphicsScene *scene = new QGraphicsScene; // scene
QGraphicsRectItem *item = new QGraphicsRectItem(100,100,50,50); // Rectangular item
scene->addItem(item); // Add item to scene
QGraphicsView *view = new QGraphicsView; // View
view->setScene(scene); // The view is associated with the scene
view->resize(500,500);
view->show(); // Show view
Specific operation
Create a new primitive class MyItem Inherit QGraphicsItem, You can implement customized element items 
Note that you need to rewrite two virtual functions boundingRect() and paint(), Otherwise there will be errors in the compilation
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;// Pen width
return QRectF(0 - penWidth/2,0-penWidth/2,20+penWidth,20+penWidth);// Return rectangle border ( coordinate 、 Wide and high )
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// Indicates that the parameter is not used
Q_UNUSED(option)
Q_UNUSED(widget)
painter->setBrush(Qt::green);// With the green Brush
painter->drawEllipse(0,0,50,50);// A circle
}
effect 1: A green circle ::
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; // scene
// QGraphicsRectItem *item = new QGraphicsRectItem(100,100,50,50); // Rectangular item
MyItem *item=new MyItem;// Create a new primitive class object
scene->addItem(item); // Add item to scene
QGraphicsView *view = new QGraphicsView; // View
view->setScene(scene); // The view is associated with the scene
view->resize(500,500);
view->show(); // Show view
return a.exec();
}


effect 2: Two green circles
operation : Then define a primitive class object , Remember to change the position of the second element ( Prevent coincidence with the first )
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; // scene
// QGraphicsRectItem *item = new QGraphicsRectItem(100,100,50,50); // Rectangular item
MyItem *item=new MyItem;
MyItem *item1=new MyItem;
scene->addItem(item); // Add item to scene
scene->addItem(item1);
item1->setPos(66,66);// Set the position of the second element
QGraphicsView *view = new QGraphicsView; // View
view->setScene(scene); // The view is associated with the scene
view->resize(500,500);
view->show(); // Show view
return a.exec();
}

Element movement
Definition advance Function to move , Need to bind timer
collision detection
边栏推荐
- php优化foreach中的sql查询
- Market Research - current market situation and future development trend of aircraft wireless intercom system
- Market Research - current situation and future development trend of sickle cell therapy Market
- 建立自己的网站(22)
- Market Research - current market situation and future development trend of total nutrition products
- 服务器响应状态码
- 图形视图框架
- 杰理之充电拔出,无法触摸开机【篇】
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
- 附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
猜你喜欢

Oracle-游标

Hanoi Tower problem

Simpleitk use - 3 Common operations

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

scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文

Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation

Kubernetes resource object introduction and common commands (4)

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
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)

UE4 游戏架构 学习笔记
随机推荐
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Oracle-游标
Basic concepts of image and deep understanding of yuv/rgb
Simpleitk use - 3 Common operations
UE4 UI adaptive screen
[001] [arm-cortex-m3/4] internal register
大话云原生之负载均衡篇-小饭馆客流量变大了
New feature of go1.18: introduce new netip Network Library
U++ 原始内存 学习笔记
Methods of adding styles to native JS
杰理之直接触摸样机的顶针反应不正常【篇】
【C 题集】of Ⅴ
wait解决僵尸进程
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
Market Research - current situation and future development trend of herringbone gear Market
【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
#include errors detected. Please update your includePath.
ArrayList analysis 2: pits in ITR, listiterator, and sublist
Server response status code
Ransack combined condition search implementation