Qt浅谈之三十七界面文字显示动态化

一、简介

        最近项目中有需求:界面显示时所有的静态数据都以动态刷新的方式显示。在原来的代码上加上几个定时器QTimer就可以实现了,处理好showEvent和hideEvent事件(总体比较简单)。
Qt浅谈之三十七界面文字显示动态化_第1张图片

二、详解

1、代码

(1)processbardynamic.h
#ifndef PROCESSBARDYNAMIC_H
#define PROCESSBARDYNAMIC_H

#include <QtGui>

class ProcessbarDynamic : public QWidget
{
    Q_OBJECT

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

protected:
    void paintEvent(QPaintEvent *event);
    void resizeEvent (QResizeEvent * event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void showEvent(QShowEvent *event);
    void hideEvent(QHideEvent *event);

private slots:
    void slotUpdateTimer();
    void slotMoneyTimer();

private:
    QPoint beginDrag;
    bool bPressFlag;
    QLabel *firstLabel;
    QLabel *secondLabel;
    QPushButton *closeBtn;
    QTimer *moneyTimer;
    QTimer *updateTimer;
    QProgressBar *progressBar;
    QLabel *progressBarLabel;
    QPushButton *sureBtn;

    int currentMoneyValue;
    int currentNumValue;
    int currentPercentValue;
};

#endif // PROCESSBARDYNAMIC_H
(2)processbardynamic.cpp
#include "processbardynamic.h"
#define MAXMONEY 660066
#define MAXNUM 15
#define MAXPERCENT 60

ProcessbarDynamic::ProcessbarDynamic(QWidget *parent)
    : QWidget(parent, Qt::FramelessWindowHint)
    , bPressFlag(false)
    , currentMoneyValue(0)
    , currentNumValue(0)
    , currentPercentValue(0)
{
    resize(300, 150);
    setAutoFillBackground(false);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, QColor(0xFF,0xFF,0xFF,0xFF));
    setPalette(pal);

    firstLabel = new QLabel(this);
    firstLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    firstLabel->setText(tr("<font color='#666666'>%1</font><font color='#FC822B' size=4><b>$%2</b></font>").arg(tr("remainingmoney")).arg("0"));

    secondLabel = new QLabel(this);
    secondLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    secondLabel->setText(QString("<font color='#666666'>%1</font><font color='#FC822B' size=4><b>%2</b></font><font color='#666666'>%3</font>").arg(tr("input")).arg(0).arg(tr("number")));

    progressBar = new QProgressBar(this);
    progressBar->setRange(0, 100);
    progressBar->reset();
    progressBar->setValue(0);
    progressBar->setTextVisible(false);

    progressBar->setStyleSheet("QProgressBar{border:none;background:#E5E5E5;border-radius:2px;}"
                              "QProgressBar::chunk{background:#009DD6;border-radius:2px;}");
    progressBarLabel = new QLabel(this);
    progressBarLabel->setStyleSheet("color:#333333;");
    progressBarLabel->setAlignment(Qt::AlignCenter);
    progressBarLabel->setFont(QFont("arial", 10, QFont::Normal));
    progressBarLabel->setText(QString("%1%").arg(0));

    sureBtn = new QPushButton(this);
    sureBtn->setFont(QFont("arial", 10, QFont::Normal));
    sureBtn->setStyleSheet("QPushButton{border:none;color:#FFFFFF;background:#009DD6;border-radius:4px;}"
                           "QPushButton:hover{border-color:green;background:#00ACF0;}");
    sureBtn->setFocusPolicy(Qt::NoFocus);
    sureBtn->setText(tr("sure"));

    moneyTimer = new QTimer(this);
    moneyTimer->setInterval(1);
    connect(moneyTimer, SIGNAL(timeout()), this, SLOT(slotMoneyTimer()));
    updateTimer = new QTimer(this);
    updateTimer->setInterval(20);
    connect(updateTimer, SIGNAL(timeout()), this, SLOT(slotUpdateTimer()));

    closeBtn = new QPushButton(this);
    closeBtn->setStyleSheet("QPushButton{background-image:url(:/close.png);border-style:flat;}"
                            "QPushButton:hover{background-image:url(:/close_hover.png);border-style:flat;}");
    closeBtn->setFocusPolicy(Qt::NoFocus);
    connect(closeBtn, SIGNAL(clicked()), this, SLOT(close()));
}

ProcessbarDynamic::~ProcessbarDynamic()
{
    if (updateTimer->isActive()) {
        updateTimer->stop();
    }
    if (moneyTimer->isActive()) {
        moneyTimer->stop();
    }
    currentMoneyValue = 0;
    currentNumValue = 0;
    currentPercentValue = 0;
}

void ProcessbarDynamic::slotUpdateTimer()
{
    if (currentNumValue <= MAXNUM) {
        secondLabel->setText(QString("<font color='#666666'>%1</font><font color='#FC822B' size=4><b>%2</b></font><font color='#666666'>%3</font>").arg(tr("input")).arg(currentNumValue).arg(tr("number")));
    }
    if (currentPercentValue <= MAXPERCENT) {
        progressBar->setValue(currentPercentValue);
        progressBarLabel->setText(QString("%1%").arg(currentPercentValue));
    }
    if (currentNumValue > MAXNUM && currentPercentValue > MAXPERCENT) {
        updateTimer->stop();
    }
    currentNumValue++;
    currentPercentValue++;
}

void ProcessbarDynamic::slotMoneyTimer()
{
    if (currentMoneyValue <= MAXMONEY) {
        QString value = "";
        int number = currentMoneyValue;
        if (number < 1000) {
            value = QString::number(number);
        }
        else if (number < 1000 * 1000) {
            value = QString::number(number/1000);
            value += ",";
            //QString temp = "";
            //temp = QString::number(number%1000);
            //temp = temp.rightJustified(3,'0');
            //temp.fill('0', 3);
            //temp.push_back(QString::number(number));
            //temp = temp.right(3);
            value += QString("%1").arg(number%1000, 3, 10, QChar('0'));
        }
        else if (number < 1000*1000*1000) {
            value = QString::number(number/(1000*1000));
            value += ",";
            number = number%(1000*1000);
            value += QString("%1").arg(number/1000, 3, 10, QChar('0'));
            value += ",";
            value += QString("%1").arg(number%1000, 3, 10, QChar('0'));

        }
        firstLabel->setText(tr("<font color='#666666'>%1</font><font color='#FC822B' size=4><b>$%2</b></font>").arg(tr("remainingmoney")).arg(value));
    }
    if (currentMoneyValue == MAXMONEY){
        moneyTimer->stop();
    }
    currentMoneyValue = currentMoneyValue + 500;
    if (currentMoneyValue > MAXMONEY) {
        currentMoneyValue = MAXMONEY;
    }
}

void ProcessbarDynamic::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
}

void ProcessbarDynamic::resizeEvent(QResizeEvent *event)
{
    firstLabel->setGeometry(15, 25, 150, 30);
    secondLabel->setGeometry(165, 25, 120, 30);
    progressBar->setGeometry(15, 73, 230, 5);
    progressBarLabel->setGeometry(255, 62, 30, 25);
    sureBtn->setGeometry(15, 90, 270, 45);
    closeBtn->setGeometry(width() - 15, 5, 10, 10);
    move((QApplication::desktop()->width() - width())/2,  (QApplication::desktop()->height() - height())/2);
    QWidget::resizeEvent(event);
}

void ProcessbarDynamic::showEvent(QShowEvent *event)
{
    currentMoneyValue = 0;
    currentNumValue = 0;
    currentPercentValue = 0;
    updateTimer->start();
    moneyTimer->start();
    QWidget::showEvent(event);
}

void ProcessbarDynamic::hideEvent(QHideEvent *event)
{
    if (updateTimer->isActive()) {
        updateTimer->stop();
    }
    if (moneyTimer->isActive()) {
        moneyTimer->stop();
    }
    QWidget::hideEvent(event);
}

void ProcessbarDynamic::mousePressEvent(QMouseEvent *event)
{
    bPressFlag = true;
    beginDrag = event->pos();
    QWidget::mousePressEvent(event);
}

void ProcessbarDynamic::mouseMoveEvent(QMouseEvent *event)
{
    if (bPressFlag) {
        QPoint relaPos(QCursor::pos() - beginDrag);
        move(relaPos);
    }
    QWidget::mouseMoveEvent(event);
}

void ProcessbarDynamic::mouseReleaseEvent(QMouseEvent *event)
{
    bPressFlag = false;
    QWidget::mouseReleaseEvent(event);
}
(3)main.cpp
#include "processbardynamic.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("processbardynamic.qm",":/");
    a.installTranslator(&translator);
    ProcessbarDynamic w;
    w.show();

    return a.exec();
}
(4)运行(隐藏再显示也会重新动态显示)
Qt浅谈之三十七界面文字显示动态化_第2张图片 Qt浅谈之三十七界面文字显示动态化_第3张图片
Qt浅谈之三十七界面文字显示动态化_第4张图片 Qt浅谈之三十七界面文字显示动态化_第5张图片

三、总结

(1)上述代码难度不大,仅提供一种显示方式。测试代码已上传到CSDN:http://download.csdn.net/detail/taiyang1987912/9347839。
(2)若有建议,请留言,在此先感谢!

你可能感兴趣的:(linux,qt)