Qt工作笔记-QGraphicsScene练习【Qt图形框架练习】

程序运行如下图所示:



程序结构如下:



代码如下:

directitem.h

#ifndef DIRECTITEM_H
#define DIRECTITEM_H

#include <QObject>
#include <QPoint>
#include <QGraphicsItem>
#include <QPixmap>

#define Direction int

class DirectItem:public QObject,public QGraphicsItem
{
    Q_OBJECT
public:
    enum{up,down,left,right};
    DirectItem(QObject *parent=0);

    void setMostUpLeft(const QPoint data);
    void setMostDownLeft(const QPoint data);
    void setMostUpRight(const QPoint data);
    void setMostDownRight(const QPoint data);

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect()const;
    void timerEvent(QTimerEvent *event);

private:
    QPixmap m_pic;

    //存4个边界左上,左下,右上,右下
    QPoint mostUpLeft,mostDownLeft,mostUpRight,mostDownRight;
    Direction flag;
};

#endif // DIRECTITEM_H

elecitem.h

#ifndef ELECITEM_H
#define ELECITEM_H

#include <QObject>
#include <QGraphicsItem>
#include <QPixmap>

class ElecItem:public QObject,public QGraphicsItem
{
    Q_OBJECT
public:
    ElecItem(QObject *parent=0);
    ~ElecItem();
    int getPicWidth()const;
    void setStatus(const bool flag);

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    QRectF boundingRect()const;

private:
    QPixmap m_close;
    QPixmap m_open;

    bool m_currStatus;
};

#endif // ELECITEM_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

class ElecItem;
class QGraphicsLineItem;
class DirectItem;
class QGraphicsScene;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void timerEvent(QTimerEvent *event);

private:
    Ui::Widget *ui;
    ElecItem *m_elecItem;
    DirectItem *m_direcItem;
    QGraphicsLineItem *lineItem1;
    QGraphicsLineItem *lineItem2;
    QGraphicsLineItem *lineItem3;
    QGraphicsLineItem *lineItem4;
    QGraphicsLineItem *lineItem5;
    QGraphicsScene *scene;
};

#endif // WIDGET_H

directitem.cpp

#include "directitem.h"
#include <Qpainter>
#include <QDebug>

DirectItem::DirectItem(QObject *parent)
    :QObject(parent)
{
    m_pic.load(":/img/power.png");
    startTimer(2);
    flag=right;
}

void DirectItem::setMostUpLeft(const QPoint data)
{
    mostUpLeft=data;
}

void DirectItem::setMostDownLeft(const QPoint data)
{
    mostDownLeft=data;
}

void DirectItem::setMostUpRight(const QPoint data)
{
    mostUpRight=data;
}

void DirectItem::setMostDownRight(const QPoint data)
{
    mostDownRight=data;
}

void DirectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(boundingRect().topLeft(),m_pic);
}

QRectF DirectItem::boundingRect() const
{
    qreal adjust =2;
    return QRectF(-m_pic.width()/2-adjust,-m_pic.height()/2-adjust,m_pic.width()+adjust*2,m_pic.height()+adjust*2);
}

void DirectItem::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)
    if(this->pos().x()>=mostUpLeft.x()&&this->pos().x()<=mostUpRight.x()&&flag==right){
        this->setPos(pos().x()+1,pos().y());
        if(pos().x()==mostUpRight.x()){
            flag=down;
        }
    }
    else if(this->pos().y()>=mostUpRight.y()&&this->pos().y()<=mostDownRight.y()&&flag==down){
        this->setPos(pos().x(),pos().y()+1);
        if(pos().y()==mostDownLeft.y()){
            flag=left;
        }
    }
    else if(this->pos().x()-1<=mostDownRight.x()&&this->pos().x()>=mostDownLeft.x()&&flag==left){
        this->setPos(pos().x()-1,pos().y());
        if(pos().x()==mostDownLeft.x()){
            flag=up;
        }
    }
    else if(flag==up){
        setPos(pos().x(),pos().y()-1);
        if(pos().y()==mostUpLeft.y()){
            flag=right;
        }
    }
}

elecitem.cpp

#include "elecitem.h"
#include <QPainter>
#include <QDebug>

ElecItem::ElecItem(QObject *parent):
    QObject(parent)
{
    m_currStatus=false;
    m_close.load(":/img/close.png");
    m_open.load(":/img/open.png");

}

ElecItem::~ElecItem()
{

}

int ElecItem::getPicWidth() const
{
    return m_open.width();
}

void ElecItem::setStatus(const bool flag)
{
    m_currStatus=flag;
}


void ElecItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    if(m_currStatus){
        painter->drawPixmap(0,0,m_open.width(),m_open.height(),m_open);
    }
    else{
        painter->drawPixmap(0,0,m_open.width(),m_open.height(),m_close);
    }
}

QRectF ElecItem::boundingRect() const
{
    return QRectF(this->pos().x()+2,this->pos().y(),m_open.width()-4,m_open.height());
}

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "elecitem.h"
#include <QGraphicsScene>
#include <QDebug>
#include "directitem.h"
#include <QGraphicsLineItem>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("CSDN IT1995");
    m_elecItem=new ElecItem;

    scene=new QGraphicsScene;
    scene->setSceneRect(QRectF(0,0,100,100));

    lineItem1=new QGraphicsLineItem;
    lineItem1->setLine(-200,65,m_elecItem->pos().x(),65);
    lineItem1->setPen(QPen(QBrush(QColor("black")),5));
    scene->addItem(lineItem1);

    lineItem2=new QGraphicsLineItem;
    lineItem2->setLine(-200,65,-200,-200);
    lineItem2->setPen(QPen(QBrush(QColor("black")),5));
    scene->addItem(lineItem2);

    lineItem3=new QGraphicsLineItem;
    lineItem3->setLine(-200,-200,200+m_elecItem->getPicWidth(),-200);
    lineItem3->setPen(QPen(QBrush(QColor("black")),5));
    scene->addItem(lineItem3);

    lineItem4=new QGraphicsLineItem;
    lineItem4->setLine(200+m_elecItem->getPicWidth(),-200,200+m_elecItem->getPicWidth(),65);
    lineItem4->setPen(QPen(QBrush(QColor("black")),5));
    scene->addItem(lineItem4);

    lineItem5=new QGraphicsLineItem;
    lineItem5->setLine(200+m_elecItem->getPicWidth(),65,m_elecItem->pos().x()+m_elecItem->getPicWidth(),65);
    lineItem5->setPen(QPen(QBrush(QColor("black")),5));
    scene->addItem(lineItem5);

    scene->addItem(m_elecItem);

    m_direcItem=new DirectItem;
    scene->addItem(m_direcItem);
    m_direcItem->setPos(-200,-200);
    ui->graphicsView->setScene(scene);

    m_direcItem->setMostUpLeft(QPoint(-200,-200));
    m_direcItem->setMostDownLeft(QPoint(-200,65));
    m_direcItem->setMostUpRight(QPoint(200+m_elecItem->getPicWidth(),-200));
    m_direcItem->setMostDownRight(QPoint(200+m_elecItem->getPicWidth(),65));
    startTimer(1);
}

Widget::~Widget()
{
    delete m_elecItem;
    delete ui;
}

void Widget::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)
    if(m_direcItem->pos().x()>=m_elecItem->pos().x()
            &&m_direcItem->pos().x()<=m_elecItem->pos().x()+m_elecItem->getPicWidth()
            &&m_direcItem->pos().y()==65){
        m_elecItem->setStatus(true);
    }
    else{
        m_elecItem->setStatus(false);
    }
    scene->update();
}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/80802843