Qt的主窗口彈出消息框


先說一下整體思路,其實很簡單主要使用到了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;
}

運行效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM