当前位置:网站首页>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
 Insert picture description here

原网站

版权声明
本文为[kin_ sixteen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141214492453.html