当前位置:网站首页>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
边栏推荐
- LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
- PXE network
- LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
- How to rapidly deploy application software under SaaS
- [book club issue 13] coding format of video files
- Preliminary exploration of flask: WSGI
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- unity update 协程_Unity 协程的原理
- %F format character
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
猜你喜欢
Scientific research cartoon | what else to do after connecting with the subjects?
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
夜天之书 #53 Apache 开源社群的“石头汤”
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
Unity脚本生命周期 Day02
What is the future of the booming intelligent Internet of things (aiot) in recent years?
In today's highly integrated chips, most of them are CMOS devices
2022年九大CIO趋势和优先事项
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
随机推荐
2022年九大CIO趨勢和優先事項
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Unity script API - component component
LeetCode 58. Length of the last word
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
How did the beyond concert 31 years ago get super clean and repaired?
Unity animation day05
. Net applications consider x64 generation
宽度精度
科研漫画 | 联系到被试后还需要做什么?
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
MySQL学习笔记——数据类型(数值类型)
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
[book club issue 13] packaging format and coding format of audio files
Audio and video technology development weekly | 252
深入JS中几种数据类型的解构赋值细节
宽度与对齐
c# 实现定义一套中间SQL可以跨库执行的SQL语句
%F format character