先說一下整體思路,其實很簡單主要使用到了Qt 的定時器,兩個QWidget窗體,消息窗我們只要讓它按着定時器的節奏把它向左移動它的寬度或向右移動它的寬度從而實現消息框的彈出與隱藏。
主要代碼:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "form.h" #include <QTimer> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; Form *smallWidget; QTimer *timer; int progressCount; bool backFlag; public slots: void widgetShow(); void changeShowflag(); void makeTimerstart(); }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); smallWidget = new Form(this); smallWidget->setGeometry(640,110,smallWidget->width(),smallWidget->height()); smallWidget->show(); timer = new QTimer(); connect(timer,SIGNAL(timeout()),this,SLOT(widgetShow())); progressCount = 740; timer->start(10); backFlag = false; connect(smallWidget,SIGNAL(backFlag()),this,SLOT(changeShowflag())); connect(smallWidget,SIGNAL(widgetShow()),this,SLOT(makeTimerstart())); } void Widget::widgetShow() { //progressCount=progressCount-25; if(!backFlag) { progressCount--; if(progressCount <= 640) { //progressCount = 740; timer->stop(); } smallWidget->setGeometry(progressCount,110,smallWidget->width(),smallWidget->height()); } else { progressCount++; if(progressCount == 740) { backFlag = false; timer->stop(); } smallWidget->setGeometry(progressCount,110,smallWidget->width(),smallWidget->height()); } } void Widget::changeShowflag() { this->backFlag = true; this->timer->start(); } void Widget::makeTimerstart() { if(progressCount>=640) { timer->start(); } } Widget::~Widget() { delete ui; }
運行效果: