1. 程式人生 > >Qt淺談之四十八視窗下方彈出提示資訊

Qt淺談之四十八視窗下方彈出提示資訊

一、簡介

      在專案中一般都會彈出新的子對話方塊顯示提示資訊,但對於一些因後臺資料變化引發的提示還是在視窗下方彈出提示資訊比較合理。點選按鈕彈出提示資訊,當滑鼠放在提示資訊對話方塊上時,暫停動畫可長時間檢視提示資訊。

二、詳解

1、程式碼

(1)faderwidget.h

#ifndef FADERWIDGET_H
#define FADERWIDGET_H
#include <QtGui>

class faderwidget : public QWidget
{
    Q_OBJECT
public:
    enum warning_type {
        error = 1, warning = 2, normal = 3
    };

    explicit faderwidget(QWidget *parent = 0);
    void showWarning(const warning_type &type, const QString &contents);
    void fixWarning(const bool &fix);
    void setMainWidget(QWidget* win);

private:
    void setFixed(const bool &fix);
    bool isFixed() { return _fixed; }

protected:
    void paintEvent(QPaintEvent *event);
    void enterEvent(QEvent *);
    void leaveEvent(QEvent *);

private:
    QTimeLine *timeline;
    int changenumber;
    qreal view_width;
    qreal view_height;
    qreal picture_heigth;
    int status;
    int tip_change_rate;
    QString _content;

    QPixmap _tipsBox_error_left;
    QPixmap _tipsBox_error_center;
    QPixmap _tipsBox_error_right;
    QPixmap _tipsBox_note_left;
    QPixmap _tipsBox_note_center;
    QPixmap _tipsBox_note_right;
    QPixmap _tipsBox_succeed_left;
    QPixmap _tipsBox_succeed_center;
    QPixmap _tipsBox_succeed_right;
    bool _running;
    bool _fixed;
    QWidget* mainWidget;

signals:

private slots:
    void fade_on_fade_in(int counter);
    void receiveStatus(int,int,QString);
    void timeFinished();
};

#endif // FADERWIDGET_H
(2)faderwidget.cpp
#include "faderwidget.h"

faderwidget::faderwidget(QWidget *parent)
    : QWidget(parent)
    , _running(false)
    , _fixed(false)
    , status(3)
{
    if (parent) {
        mainWidget = parent;
        this->view_width = mainWidget->width();
        this->view_height = mainWidget->height();
        setGeometry(QRect(0, -33, mainWidget->width(), 33));
    }
    setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    setStyleSheet("background-color:rgba(255,255,255,0)");
    QTextCodec *codec = QTextCodec::codecForName("System");
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);

    timeline=new QTimeLine(3000, this);
    timeline->setFrameRange(0,300);
    changenumber = 0;
    picture_heigth = 30;

    _tipsBox_error_center.load(":/tipsBox_error_center.png");
    _tipsBox_note_center.load(":/tipsBox_note_center.png");
    _tipsBox_succeed_center.load(":/tipsBox_succeed_center.png");
    connect(timeline,SIGNAL(frameChanged(int)),this,SLOT(fade_on_fade_in(int)));
    connect(timeline, SIGNAL(finished()), this, SLOT(timeFinished()));
    hide();
}

void faderwidget::showWarning(const faderwidget::warning_type &type, const QString &contents)
{
    receiveStatus(type, 20 , contents);
    this->setFocus();
}
void faderwidget::fixWarning(const bool &fix)
{
    if (fix)  setFixed(fix);
}

void faderwidget::setMainWidget(QWidget *win)
{
    if (win) {
        mainWidget = win;
        this->view_width = mainWidget->width();
        this->view_height = mainWidget->height();
        setGeometry(QRect(0, -33, mainWidget->width(), 33));
    }
}

void faderwidget::setFixed(const bool &fix)
{
    _fixed = fix;
    if (!fix && timeline->state() == QTimeLine::Paused)
        timeline->resume();
}

void faderwidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pixmapTwo;
    QString string;

    switch (status){
    case 1:
        pixmapTwo = _tipsBox_error_center;
        string=QString(tr("%1").arg(_content));
        break;
       case 2:
        pixmapTwo = _tipsBox_note_center;
        string=QString(tr("%1").arg(_content));
         break;
       case 3:
        pixmapTwo = _tipsBox_succeed_center;
        string=QString(tr("%1").arg(_content));
        break;
    }

    painter.setPen(QPen(QColor(255,255,255)));
    pixmapTwo = pixmapTwo.scaled(view_width,picture_heigth);
    painter.drawPixmap(0,0,view_width,picture_heigth,pixmapTwo);
    int textWidth = painter.fontMetrics().width(string);
    if(textWidth < 1)
        return;
    if(textWidth < view_width)
        painter.drawText((view_width-textWidth)/2,20,string);
    if(textWidth > view_width)
        painter.drawText(0,20,string);
}

void faderwidget::fade_on_fade_in(int counter)
{
    this->setVisible(true);
    changenumber=counter;

    if(changenumber<=10)
    {
        this->move(0,view_height-changenumber*picture_heigth/10);
    }
    else if(changenumber<250)
    {
        this->move(0,view_height-picture_heigth);
        if (_fixed) {
            timeline->setPaused(true);
        }
    }
    else if(changenumber<300)
    {
        this->move(0,view_height-3*picture_heigth+changenumber*picture_heigth/100);
    }
    else
    {
     timeline->stop();
     this->setVisible(false);
    }
}

void faderwidget::receiveStatus(int status,int tip_change_rate,QString content)
{
    if (_fixed)  return;
    timeline->stop();
    this->status = status;
    this->tip_change_rate = tip_change_rate;
    timeline->setUpdateInterval(this->tip_change_rate);
    timeline->setDuration(300*(this->tip_change_rate));
    timeline->setCurrentTime(0);
    this->_content=content;
    timeline->start();
    _running = true;
}

void faderwidget::timeFinished()
{
    _running = false;
}
void faderwidget::enterEvent(QEvent *)
{
    if (_running)  timeline->setPaused(true);
}

void faderwidget::leaveEvent(QEvent *)
{
    if (_running)  timeline->resume();
}
(3)widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "faderwidget.h"

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void slotClicked();

private:
    faderwidget *fw;
    QPushButton *btn;
};

#endif // WIDGET_H
(4)widget.cpp
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint)
{
    resize(400, 400);
    this->setStyleSheet("background:#19649F");
    fw = new faderwidget(this);
    btn = new QPushButton(this);
    btn->move(150, 20);
    btn->setText(tr("click"));
    btn->setStyleSheet("background:yellow;color:#FF0000");
    btn->setFocusPolicy(Qt::NoFocus);
    connect(btn, SIGNAL(clicked()), this, SLOT(slotClicked()));
    move(100, 100);
}

Widget::~Widget()
{

}

void Widget::slotClicked()
{
    fw->showWarning(faderwidget::warning, tr("the information tips"));
    //fw->fixWarning(true);
    fw->show();
}
(5)main.cpp
#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

三、總結

(1)上述程式碼還不完善,還需進一步優化,僅提供思路參考。
(2)若有問題或建議,請留言,在此感謝!