class ArrowWidget : public QWidget{ Q_OBJECTpublic: ArrowWidget(QWidget *parent = 0); virtual ~ArrowWidget() {} void paintEvent(QPaintEvent *pe); bool isDown(); bool isUp(); void setDown(); void setUp(); void setGoingUp(bool b); bool isGoingUp(); void setGoingDown(bool b); bool isGoingDown();private: bool posArrows; // 0 - down; 1 - up bool goingUp; bool goingDown; QPropertyAnimation *animation;};
ArrowWidget::ArrowWidget(QWidget *parent) : QWidget(parent){ resize(parent->size()); posArrows = false; animation = new QPropertyAnimation(this, "geometry"); animation->setDuration(1000); animation->setLoopCount(-1); animation->setStartValue(QRect(0, 0, this->width(), this->height()));// setAutoFillBackground(true);// setAttribute(Qt::WA_OpaquePaintEvent,true);}void ArrowWidget::paintEvent(QPaintEvent *pe){ QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.setBrush(QBrush(Qt::black)); painter.setPen(QPen(Qt::black)); const int arrowsNum = (height()/13); QPolygon arrows[arrowsNum]; if(!posArrows) { for(int i = 0; i < arrowsNum; i++) { arrows[i] << QPoint(1,(11*i)+2+(i*2)) << QPoint(19,(11*i)+2+(i*2)) << QPoint(10,(11*(i+1))+2+i*2); painter.drawPolygon(arrows[i]); } } else { for(int i = 0; i < arrowsNum; i++) { arrows[i] << QPoint(1,(11*(i+1))+2+(i*2)) << QPoint(19,(11*(i+1))+2+(i*2)) << QPoint(10,(11*i)+2+i*2); painter.drawPolygon(arrows[i]); } }}bool ArrowWidget::isUp(){ if(posArrows == true) return true; else return false;}void ArrowWidget::setUp(){ posArrows = true; update();}void ArrowWidget::setGoingUp(bool b){ setUp(); animation->setEndValue(QRect(0, -13, this->width(), this->height())); if(b) { animation->stop(); animation->start(); } else { animation->stop(); update(); }}bool ArrowWidget::isGoingUp(){ return goingUp;}
class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); void on_pushButton_3_clicked(); void on_pushButton_2_clicked(); void on_pushButton_4_clicked();private: Ui::MainWindow *ui; ArrowWidget *myWidget;};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); //Если сделать родителем ui->widget, и закомментировать строчки ниже, стрелочки будут мерцать myWidget = new ArrowWidget(ui->graphicsView); QGraphicsScene *scene = new QGraphicsScene; ui->graphicsView->setScene(scene); ui->graphicsView->setBackgroundBrush(QColor(240,240,240)); scene->addWidget(myWidget);}
animation->stop();
animation->start();
QPolygon arrows; for(int i = 0; i < arrowsNum; i++) { arrows.clear(); arrows << QPoint(1,(11*i)+2+(i*2)) << QPoint(19,(11*i)+2+(i*2)) << QPoint(10,(11*(i+1))+2+i*2); painter.drawPolygon(arrows); }