当前位置:网站首页>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
边栏推荐
- Unity prefab day04
- LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
- Redis哨兵模式实现一主二从三哨兵
- 【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
- Selenium element interaction
- Summer Review, we must avoid stepping on these holes!
- 案例分享|金融业数据运营运维一体化建设
- 从0到1建设智能灰度数据体系:以vivo游戏中心为例
- MySQL federated primary key_ MySQL creates a federated primary key [easy to understand]
- MySQL组合索引(多列索引)使用与优化案例详解
猜你喜欢
2022年九大CIO趋势和优先事项
这几年爆火的智能物联网(AIoT),到底前景如何?
大神详解开源 BUFF 增益攻略丨直播
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
案例分享|金融业数据运营运维一体化建设
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
How did the beyond concert 31 years ago get super clean and repaired?
夜天之书 #53 Apache 开源社群的“石头汤”
Summer Review, we must avoid stepping on these holes!
华为云数据库DDS产品深度赋能
随机推荐
.Net 应用考虑x64生成
[learning notes] matroid
The per capita savings of major cities in China have been released. Have you reached the standard?
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
Understand the context in go language in an article
%S format character
How to rapidly deploy application software under SaaS
Redis哨兵模式实现一主二从三哨兵
数据湖治理:优势、挑战和入门
Align left and right!
输入宽度!
Unity prefab day04
2022年九大CIO趋势和优先事项
Logstash ~ detailed explanation of logstash configuration (logstash.yml)
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
小数,指数
大神详解开源 BUFF 增益攻略丨直播
华为云数据库DDS产品深度赋能
這幾年爆火的智能物聯網(AIoT),到底前景如何?