当前位置:网站首页>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 
边栏推荐
- Summary of database 2
- lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
- Redis shares four cache modes
- How to rapidly deploy application software under SaaS
- 一篇文章搞懂Go语言中的Context
- 【读书会第十三期】 音频文件的封装格式和编码格式
- Salient map drawing based on OpenCV
- Rearrange array
- Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
- The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
猜你喜欢

科研漫画 | 联系到被试后还需要做什么?

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example

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

案例分享|金融业数据运营运维一体化建设

这几年爆火的智能物联网(AIoT),到底前景如何?

MySQL index optimization

In today's highly integrated chips, most of them are CMOS devices

力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
Redis sentinel mode realizes one master, two slave and three Sentinels

Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
随机推荐
Function test - knowledge points and common interview questions
谈SaaS下如何迅速部署应用软件
Align left and right!
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
这几年爆火的智能物联网(AIoT),到底前景如何?
Temperature control system based on max31865
开源人张亮的 17 年成长路线,热爱才能坚持
深入JS中几种数据类型的解构赋值细节
How can floating point numbers be compared with 0?
AI has surpassed Dr. CS in question making?
PXE network
Recommend 10 excellent mongodb GUI tools
C implementation defines a set of intermediate SQL statements that can be executed across libraries
An article learns variables in go language
Implementation of web chat room
【大连理工大学】考研初试复试资料分享
The four most common errors when using pytorch
Feature extraction and detection 15-akaze local matching
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
宽度与对齐