当前位置:网站首页>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
边栏推荐
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
- 牛客网:龙与地下城游戏
- 杰理之样机在多次触摸后会触发关机【篇】
- Oracle cursor
- NC50965 Largest Rectangle in a Histogram
- Pointer array parameter passing, pointer parameter passing
- U++ learning note pile
- Server response status code
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Regular expression (2)
猜你喜欢

Task and privilege level protection
![[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')](/img/02/67448df1817e8b34b654722df8ecd4.jpg)
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')

大话云原生之负载均衡篇-小饭馆客流量变大了

phpcms实现订单直接支付宝支付功能

百度智能云-创建人脸识别应用

Dynamic memory allocation (malloc calloc realloc free)
![[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)](/img/2b/f31b81cedf37ca187bcaa20dfe0b83.png)
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)

Oracle-PL/SQL编程

NC50965 Largest Rectangle in a Histogram

PMP项目整合管理
随机推荐
phpcms实现订单直接支付宝支付功能
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
Oracle PL / SQL programming
数据库系统概论第一章简答题-期末考得怎么样?
[QT] QT multithreading development - four methods to realize multithreading design
杰理之内置关机电流 1.2uA,之后不能长按开机【篇】
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
PHP optimizes SQL queries in foreach
U++ learning note pile
Utilisation de simpletk - 4. Question étrange
[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)
牛客网:龙与地下城游戏
图形视图框架
Socket socket c/s end process
Pointer and string
LxC terminal login method
PMP项目整合管理
App page sharing password rails implementation
杰理之内置短按再长按,不管长按多长时间都是短按【篇】