当前位置:网站首页>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
边栏推荐
- 钟薛高回应产品1小时不化:含固体成分 融化不能变成水
- 杰理之修改不需要长按开机功能【篇】
- JS获取display为none的隐藏元素的宽度和高度的解决方案
- Market Research - current situation and future development trend of environmental friendly fireworks Market
- 《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
- 540. Single element in ordered array
- [QT] QT multithreading development - reentrancy and thread safety
- Ransack combined condition search implementation
- 杰理之样机无触摸,拆机之后重新安装变正常【篇】
- Phpcms realizes the direct Alipay payment function of orders
猜你喜欢
Source code analysis - lightweight asynchronous crawler framework Ruia
Oracle cursor
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Oracle-PL/SQL编程
Micro service gateway selection, please accept my knees!
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Dynamic memory allocation (malloc calloc realloc free)
540. Single element in ordered array
Radis:Linux上安装Redis(步骤)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
随机推荐
杰理之内置短按再长按,不管长按多长时间都是短按【篇】
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Mathematical modeling -- graph and network models and methods (I)
Market Research - current situation and future development trend of carob chocolate market
数据库系统概论第一章简答题-期末考得怎么样?
php优化foreach中的sql查询
傑理之修改不需要長按開機功能【篇】
Market Research - current situation and future development trend of marine clutch Market
U++ learning note pile
NC24325 [USACO 2012 Mar S]Flowerpot
Try to get property'num for PHP database data reading_ rows' of non-object?
JS solution for obtaining the width and height of hidden elements whose display is none
Objects and object variables
Market Research - current situation and future development trend of herringbone gear Market
杰理之样机在多次触摸后会触发关机【篇】
Unity3d learning notes 4 - create mesh advanced interface
Commodity information management system (C language document version)
Market Research - current situation and future development trend of sickle cell therapy Market
U++ 原始内存 学习笔记
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂