#include "graphwidget.h" #include <QGraphicsScene> #include <QRectF> #include <QPainterPath> #include <QtCore/qmath.h> #include "base.h" GraphWidget::GraphWidget() { //设置更新时间,(如果时间太长,也就是更新太慢,当speed太小时会导致更新比鼠标运动还要慢,出错)、 timerId = startTimer(1000 / 80.0);//设置更新的时间间隔 FRECUENCY = 100 QGraphicsScene *scene = new QGraphicsScene(this); scene->setItemIndexMethod(QGraphicsScene::NoIndex); scene->setSceneRect(-300, -150, 600, 600); setMinimumSize(800, 800); setScene(scene);// setCacheMode(CacheBackground); setViewportUpdateMode(BoundingRectViewportUpdate); setRenderHint(QPainter::Antialiasing); setTransformationAnchor(AnchorUnderMouse); setResizeAnchor(AnchorViewCenter); //新增加baseLeft baseLeft =new base(this); scene->addItem(baseLeft); baseLeft->setPos(-300,0); QGraphicsRectItem * baserect = new QGraphicsRectItem(-40,-40,80,80); baserect->setPen(QPen(Qt::black)); baserect->setPos(-300,0); scene->addItem(baserect); QGraphicsEllipseItem * baseRound = new QGraphicsEllipseItem(-30,-30,60,60); baseRound->setPen(QPen(Qt::black)); baseRound->setPos(-300,0); scene->addItem(baseRound); //新增加baseRight baseRight =new base(this); scene->addItem(baseRight); baseRight->setPos(300,0); QGraphicsRectItem * baserectbaseRight = new QGraphicsRectItem(-40,-40,80,80); baserectbaseRight->setPen(QPen(Qt::black)); baserectbaseRight->setPos(300,0); scene->addItem(baserectbaseRight); QGraphicsEllipseItem * baseRoundbaseRight = new QGraphicsEllipseItem(-30,-30,60,60); baseRoundbaseRight->setPen(QPen(Qt::black)); baseRoundbaseRight->setPos(300,0); scene->addItem(baseRoundbaseRight); //新增加centerbase centerbase =new Centerbase(this); scene->addItem(centerbase); centerbase->setPos(-0,0); //新增加underbase underbase =new Underbase(this); scene->addItem(underbase); underbase->setPos(-0,0); //新增加线条 for(int i=0;i<8;i++) { lines[i] = new QGraphicsRectItem(); lines[i]->setPen(QPen(Qt::black)); lines[i]->setPos(0,0); } SetLinesPos(20,20); for(int n=0;n<8;n++) { scene->addItem(lines[n]); } Drawitems(0,0); //新增加轨迹 QGraphicsRectItem * baserect2 = new QGraphicsRectItem(-100,0,200,160); baserect2->setPen(QPen(Qt::blue)); baserect2->setPos(-0,380); scene->addItem(baserect2); QGraphicsEllipseItem * baseRound2 = new QGraphicsEllipseItem(-40*2,0,80*2,80*2); baseRound2->setPen(QPen(Qt::blue)); baseRound2->setPos(-0,380); scene->addItem(baseRound2); scale(qreal(0.8), qreal(0.8)); } //线条平移距离,相对最初位置 void GraphWidget::SetLinesPos(qreal x,qreal y) { lines[0]->setRect(-300,-30,300-60+x,2); lines[1]->setRect(-300,30,300-60+x,2); lines[2]->setRect(60+x,-30,300-60-x,2); lines[3]->setRect(60+x,30,300-60-x,2); lines[4]->setRect(-30+x,-300-y,2,300-60+y); lines[5]->setRect(30+x,-300-y,2,300-60+y); lines[6]->setRect(-30+x,60,2,300-60-y); lines[7]->setRect(30+x,60,2,300-60-y); } void GraphWidget::Drawitems(qreal x,qreal y) { centerbase->setPos(x,0); underbase->setPos(x,-y); SetLinesPos(x,y); } void GraphWidget::timerEvent(QTimerEvent *event) { Q_UNUSED(event); Drawitems(underbase->pos().x(),-underbase->pos().y()); qreal x =underbase->pos().x(); qreal y = -underbase->pos().y(); qreal angle1= (x+y)/30.0; qreal angle2 = (x-y)/30.0; baseLeft->setRotation(angle1*180); baseRight->setRotation(angle2*180); static unsigned int num =0; static unsigned int status = 0; static unsigned int thresh =100; num++; if(num>thresh) { num = 0; status++; } if(status == 0) { underbase->moveBy(1,0); } if(status == 1) {thresh =80; underbase->moveBy(0,1); } if(status == 2) { underbase->moveBy(0,1); } if(status == 3) {thresh =100; underbase->moveBy(-1,0); } if(status == 4) { underbase->moveBy(-1,0); } if(status == 5) {thresh =80; underbase->moveBy(0,-1); } if(status == 6) { underbase->moveBy(0,-1); } if(status == 7) {thresh =100; underbase->moveBy(1,0); } if(status == 8) {thresh = 2*3.141592653*80; underbase->moveBy(qCos(2*3.141592653*num*1.0/thresh),qSin(2*3.141592653*num*1.0/thresh)); } }
1 #include <QApplication> 2 #include "mainwindow.h" 3 #include "graphwidget.h" 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 GraphWidget w; 9 w.show(); 10 11 return a.exec(); 12 }
1 #include <QGraphicsScene> 2 #include <QGraphicsSceneMouseEvent> 3 #include <QPainter> 4 #include <QStyleOption> 5 6 #include "underbase.h" 7 #include "graphwidget.h" 8 9 #include <QtCore/qmath.h> 10 #include <qDebug> 11 12 Underbase::Underbase(GraphWidget *graphWidget) : graph(graphWidget) 13 { 14 setFlag(ItemIsMovable); 15 setCacheMode(DeviceCoordinateCache); 16 setZValue(1); 17 } 18 19 QRectF Underbase::boundingRect() const 20 { 21 return QRectF(-110,-300,220,380+300); 22 } 23 24 QPainterPath Underbase::shape() const 25 { 26 QPainterPath path; 27 path.addRect(-110,-300,220,380+300); 28 return path; 29 30 31 } 32 33 void Underbase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) 34 { 35 36 // painter->setPen(Qt::black); 37 // //painter->setBrush(Qt::red); 38 // painter->drawLine(0,0,30,0); 39 //painter->drawRect(-100,-100,200,200); 40 // painter->drawEllipse(-30-60, -30-60, 60, 60); 41 // painter->drawEllipse(-30-60, -30+60, 60, 60); 42 // painter->drawEllipse(-30+60, -30-60, 60, 60); 43 // painter->drawEllipse(-30+60, -30+60, 60, 60); 44 45 46 painter->setPen(Qt::black); 47 //painter->setBrush(Qt::red); 48 // painter->drawLine(0,0,30,0); 49 painter->drawRect(-110,-300,220,360+300); 50 painter->drawRect(-0,+360,2,20); 51 painter->drawEllipse(-0-30, 300-30, 60, 60); 52 painter->setBrush(Qt::black); 53 painter->drawRect(-110,-300,220,5); 54 }
原文:http://www.cnblogs.com/tangyuanjie/p/6492802.html