qgraphicsscene
程序运行如下图所示:
程序结构如下:
代码如下:
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();
}
相关阅读
Qt和Qt Creator的区别 Qt是C++的一个库,或者说是开发框架,里面集成了一些库函数,提高开发效率。 Qt Creator是一个IDE,就是一个平台,一
0.前言 Qt中有多种实现多线程的方式,我最常用的有moveToThread()和QtConcurrent::run()两种方式。一般长时间存在的线程我用moveT
Qt编写项目作品大全(录音播放控件+PDF阅读器+定制化安装包工具+斗图神器等) 文章目录Qt编写项目作品大全(录音播放控件+PDF阅读器+定
关于QtGraphics编程的几点经验总结_qgraphicsscene
转载自:http://www.niubb.com/riji/2015/10-01/12524.htmlQtGraphics模块用于显示二维的图形图像,所以三维的事情就不要找它了,应该
QT/C++从新手到老手系列之QT中级篇-李浩林-专题视频课
QT/C++从新手到老手系列之QT中级篇—309人已学习 课程介绍 本阶段主要带领同学们完成以下几个方面的学习:文件、目录、输入