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;
}

运行效果:

原文地址:https://www.cnblogs.com/onlycxue/p/2755636.html