当前位置:网站首页>QT graphical view frame: element movement
QT graphical view frame: element movement
2022-07-04 15:54:00 【kin_ sixteen】
animation.h
#ifndef ANIMATION_H
#define ANIMATION_H
#include <QGraphicsScene> // scene : Add elements void addItem(QGraphicsItem *item)
#include <QGraphicsView> // View ; Show scene
#include <QGraphicsItem> // Primitives
#include "myitem.h"
#include <QTimer>
/* View ; Show scene QGraphicsScene scene; scene.addText("Hello, world!"); QGraphicsView view(&scene); view.show(); */
class animation : public QGraphicsView
{
Q_OBJECT
public:
animation();
QGraphicsScene *scene;// scene
// QGraphicsView *view;// View
MyItem *item;// Primitives 1
MyItem *item1;// Primitives 2
QTimer *time1;
//public slots:
// void updateslot();
};
#endif // ANIMATION_H
animation.cpp
#include "animation.h"
#include <QDebug>
animation::animation()
{
scene = new QGraphicsScene; // scene
item=new MyItem;
item1=new MyItem;
time1=new QTimer;
this->setScene(scene);// The view is associated with the scene
scene->addItem(item); // Elements are added to the scene
scene->addItem(item1);
connect(time1, SIGNAL(timeout()), scene, SLOT(advance()));
time1->start(1000);
}
//void animation::updateslot()
//{
// qDebug()<<" Timer "<<endl;
//}
myitem.h
#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsItem>
#include <QString>
class MyItem : public QObject,public QGraphicsItem
{
Q_OBJECT
public:
MyItem();
MyItem(QString filename);
QRectF boundingRect() const override;// Drawing range of elements : Properties of rectangular border
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;// Properties of elements
// attribute Element type File name
int itype;
QString filename;
// Realize element movement
void advance(int phase) override;
};
#endif // MYITEM_H
myitem.cpp
#include "myitem.h"
#include <QPainter>
#include <QDebug>
MyItem::MyItem()
{
}
MyItem::MyItem(QString filename)
{
// Add elements
}
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,100,100);// A circle
}
void MyItem::advance(int phase)
{
if(!phase) return; // If phase by 0, Indicates that it will start moving, then it returns
moveBy(0,10);// Move
qDebug()<<"advance";
}
main.cpp
#include "widget.h"
#include <QApplication>
#include "animation.h"
#include <QDebug>
#include "myitem.h"
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
animation animat;
animat.show();
return a.exec();
}
effect :
The green ball moves down at a fixed time 
边栏推荐
- C implementation defines a set of intermediate SQL statements that can be executed across libraries
- 函数式接口,方法引用,Lambda实现的List集合排序小工具
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
- 压力、焦虑还是抑郁? 正确诊断再治疗
- [native JS] optimized text rotation effect
- Shell programming basics
- LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
- web聊天室实现
- Temperature control system based on max31865
- How did the beyond concert 31 years ago get super clean and repaired?
猜你喜欢

夜天之书 #53 Apache 开源社群的“石头汤”

How was MP3 born?
![[native JS] optimized text rotation effect](/img/50/3c09f223e821c14e7e9e0fb47622b6.jpg)
[native JS] optimized text rotation effect

Book of night sky 53 "stone soup" of Apache open source community
![[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure](/img/f0/12dd17e840a23dc9ded379e1fd7454.jpg)
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure

MySQL学习笔记——数据类型(2)
Redis sentinel mode realizes one master, two slave and three Sentinels

31年前的Beyond演唱会,是如何超清修复的?

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"

直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
随机推荐
宽度精度
MySQL组合索引(多列索引)使用与优化案例详解
odoo数据库主控密码采用什么加密算法?
Redis shares four cache modes
中国主要城市人均存款出炉,你达标了吗?
小数,指数
Hexadecimal form
Feature extraction and detection 15-akaze local matching
%S format character
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
Essential basic knowledge of digital image processing
Understand the context in go language in an article
Rearrange array
Recommend 10 excellent mongodb GUI tools
Book of night sky 53 "stone soup" of Apache open source community
LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
谈SaaS下如何迅速部署应用软件
科普达人丨一文看懂阿里云的秘密武器“神龙架构”