效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/a4274e426c1431fe1b00e4a980620f32.jpeg)
中间的等待效果就是利用图标的切换实现的,那么今天咱们也实现类似的效果:
效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/5fdb9b50b0838348b87993af5dce9311.jpeg)
使用图标如下:
![](https://i-blog.csdnimg.cn/blog_migrate/07828f8b7b2a0ff46d2af0be3f655e83.jpeg)
代码如下:
#include "loading_widget.h"
LoadingWidget::LoadingWidget(QWidget *parent)
: QWidget(parent)
{
index = 1;
loading_label = new QLabel();
account_label = new QLabel();
waiting_label = new QLabel();
QPixmap pixmap(QString(":/loginDialog/") + QString::number(index, 10));
loading_label->setFixedSize(pixmap.size());
loading_label->setPixmap(pixmap);
loading_label->setScaledContents(true);
QVBoxLayout *v_layout = new QVBoxLayout();
v_layout->addWidget(account_label);
v_layout->addWidget(waiting_label);
v_layout->setSpacing(15);
v_layout->setContentsMargins(0, 0, 0, 0);
QHBoxLayout *h_layout = new QHBoxLayout();
h_layout->addStretch();
h_layout->addWidget(loading_label);
h_layout->addLayout(v_layout);
h_layout->addStretch();
h_layout->setSpacing(20);
h_layout->setContentsMargins(0, 0, 0, 30);
timer = new QTimer(this);
timer->setInterval(100);
connect(timer, &QTimer::timeout, this, &LoadingWidget::updatePixmap);
this->setLayout(h_layout);
this->translateLanguage();
}
LoadingWidget::~LoadingWidget()
{
}
void LoadingWidget::translateLanguage()
{
waiting_label->setText(tr("waiting"));
}
void LoadingWidget::setAccount(QString account)
{
this->account = account;
}
void LoadingWidget::start(bool is_start)
{
if(is_start)
{
//进行账号的转化,防止过长显示不全,过长用...代替
QString elided_text;
bool is_elided = Util::updateText(account, 200, elided_text);
if(!is_elided)
{
elided_text = account;
}
account_label->setText(tr("hi") + elided_text);
timer->start();
}
else
{
timer->stop();
index = 1;
loading_label->setPixmap(QPixmap(QString(":/loginDialog/") + QString::number(index, 10)));
}
}
void LoadingWidget::updatePixmap()
{
//若当前图标下标超过8表示到达末尾,则重新计数
index++;
if(index > 8)
{
index = 1;
}
QPixmap pixmap(":/loginDialog/" + QString::number(index, 10));
loading_label->setPixmap(pixmap);
}
注: