当前位置:网站首页>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 
边栏推荐
- Explore mongodb - mongodb compass installation, configuration and usage introduction | mongodb GUI
- Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
- 科研漫画 | 联系到被试后还需要做什么?
- 小数,指数
- Decimal, exponential
- Salient map drawing based on OpenCV
- %F format character
- Width and alignment
- [native JS] optimized text rotation effect
- MySQL - MySQL adds self incrementing IDs to existing data tables
猜你喜欢

MySQL学习笔记——数据类型(数值类型)

Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?

Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
MySQL组合索引(多列索引)使用与优化案例详解

Preliminary exploration of flask: WSGI
Detailed explanation of MySQL composite index (multi column index) use and optimization cases

华为云数据库DDS产品深度赋能
![[Dalian University of technology] information sharing of postgraduate entrance examination and re examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination

C1 certification learning notes 3 -- Web Foundation

函数式接口,方法引用,Lambda实现的List集合排序小工具
随机推荐
谈SaaS下如何迅速部署应用软件
Unity脚本API—Component组件
科研漫画 | 联系到被试后还需要做什么?
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
[learning notes] matroid
十六进制
深入JS中几种数据类型的解构赋值细节
Object distance measurement of stereo vision
Usage of database functions "recommended collection"
MySQL学习笔记——数据类型(数值类型)
基于MAX31865的温度控制系统
宽度精度
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
Unity动画Animation Day05
Feature extraction and detection 15-akaze local matching
[book club issue 13] packaging format and coding format of audio files
Summer Review, we must avoid stepping on these holes!
Find numbers
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
JS tile data lookup leaf node