当前位置:网站首页>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
边栏推荐
- [C question set] of V
- PHP implements querying the data matching the date of birth according to the entered age
- Reading experience of just because
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- Market Research - current situation and future development trend of anti-counterfeiting label market
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- Oracle-游标
- 图形视图框架
- 《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
猜你喜欢

New feature of go1.18: trylock, which has been tossed n times

SimpleITK使用——4. 奇怪的問題

Dynamic memory allocation (malloc calloc realloc free)
![[shutter] shutter resource file use (import resource pictures | use image resources)](/img/e9/94ae2e3ee315f490eb3cf14bcf2e49.jpg)
[shutter] shutter resource file use (import resource pictures | use image resources)

Basic concepts of image and deep understanding of yuv/rgb

Struct, bit segment, enumeration, union

wait解决僵尸进程

Socket套接字C/S端流程

Utilisation de simpletk - 4. Question étrange

Ransack combined condition search implementation
随机推荐
U++ 原始内存 学习笔记
I admire that someone explained such an obscure subject as advanced mathematics so easily
数据库系统概论第一章简答题-期末考得怎么样?
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
Web侧防御指南
建立自己的网站(22)
UE4 UI adaptive screen
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Necessary browser plug-ins for network security engineers
杰理之快速触摸不响应问题【篇】
Notes on key vocabulary in the English original of the biography of jobs (11) [chapter nine]
Build your own website (22)
Market Research - current situation and future development trend of anti-counterfeiting label market
Market Research - current market situation and future development trend of total nutrition products
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
wait解决僵尸进程
App page sharing password rails implementation
PHP微信抢红包的算法
Phpcms realizes the direct Alipay payment function of orders