QProgressBar
1.进度条
2.功能类似QSlider
QDialog 示例
1.加载进度显示
QDialog渐变透明关闭窗口
1.10 0.0 setWindowOpacity
2.QEventLoop loop;
3.loop.processEvents();
ui_progressdialog.h
#ifndef UI_PROGRESSDIALOG_H
#define UI_PROGRESSDIALOG_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QLabel>
#include <QtWidgets/QProgressBar>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_ProgressDialog
{
public:
QWidget *widget;
QProgressBar *progressBar;
QLabel *label;
void setupUi(QDialog *ProgressDialog)
{
if (ProgressDialog->objectName().isEmpty())
ProgressDialog->setObjectName(QString::fromUtf8("ProgressDialog"));
ProgressDialog->resize(700, 200);
ProgressDialog->setStyleSheet(QString::fromUtf8("#widget{\n"
" background-color: qlineargradient(spread:reflect, x1:1, y1:0.533, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));\n"
" border:1px groove rgb(232,232,232);\n"
" border-radius:20;\n"
"}"));
widget = new QWidget(ProgressDialog);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(0, 0, 700, 200));
widget->setStyleSheet(QString::fromUtf8(""));
progressBar = new QProgressBar(widget);
progressBar->setObjectName(QString::fromUtf8("progressBar"));
progressBar->setGeometry(QRect(40, 40, 601, 121));
progressBar->setMaximum(1000);
progressBar->setValue(24);
label = new QLabel(widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(320, -3, 67, 40));
retranslateUi(ProgressDialog);
QMetaObject::connectSlotsByName(ProgressDialog);
} // setupUi
void retranslateUi(QDialog *ProgressDialog)
{
ProgressDialog->setWindowTitle(QCoreApplication::translate("ProgressDialog", "ProgressDialog", nullptr));
label->setText(QCoreApplication::translate("ProgressDialog", "TextLabel", nullptr));
} // retranslateUi
};
namespace Ui {
class ProgressDialog: public Ui_ProgressDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_PROGRESSDIALOG_H
progressdialog.cpp
#include "progressdialog.h"
#include "ui_progressdialog.h"
ProgressDialog::ProgressDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::ProgressDialog)
{
ui->setupUi(this);
//去掉标题栏
this->setWindowFlags(Qt::FramelessWindowHint);
//设置背景透明
this->setAttribute(Qt::WA_TranslucentBackground,true);
}
void ProgressDialog::SetPos(int pos)
{
ui->progressBar->setValue(pos);
if(pos == 1000)
{
for(int i = 100; i > 0; i--)
{
//透明度1.0 0.0
double value = (double)i / 100.0;
qDebug()<<value;
this->setWindowOpacity(value);
QThread::msleep(30);
ui->label->setText(QString::number(i));
QEventLoop loop;
loop.processEvents();
}
this->close();
}
}
ProgressDialog::~ProgressDialog()
{
delete ui;
}
TestPro.cpp
#include "TestPro.h"
TestPro::TestPro()
:QThread()
{
}
TestPro::~TestPro()
{
}
void TestPro::run()
{
//模拟
for(int i = 0; i <= 1000; i++)
{
SetPos(i);
msleep(10);
}
}