Qt工作笔记-QGraphicsScene练习【Qt图形框架练习】
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-QGraphicsScene练习【Qt图形框架练习】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
程序運行如下圖所示:
程序結構如下:
代碼如下:
directitem.h
#ifndef DIRECTITEM_H #define DIRECTITEM_H#include <QObject> #include <QPoint> #include <QGraphicsItem> #include <QPixmap>#define Direction intclass 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_Helecitem.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_Hwidget.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_OBJECTpublic: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_Hdirectitem.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(); }總結
以上是生活随笔為你收集整理的Qt工作笔记-QGraphicsScene练习【Qt图形框架练习】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-对QObject::con
- 下一篇: Qt-IP地址查询工具(使用HTTP G