qt 进度条
2016-03-02 16:00
405 查看
转自http://blog.csdn.net/aaa20090987/article/details/7664634
当程序在执行一项(或多项)耗时比较久的操作时,界面总要有一点东西告诉用户“程序还在运行中”,那么,一个“没有终点”的进度条就是你需要的了。
PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程,造成程序卡死的假象。
思路:程序很简单,一个进度条,一个定时器就足够了。
截图:
源代码:
[cpp] view
plaincopyprint?
#include <QtCore>
#include <QtGui>
class WaitingDialog : public QDialog
{
Q_OBJECT
private:
int m_CurrentValue; //当前值
int m_UpdateInterval; //更新间隔
int m_MaxValue; //最大值
QTimer m_Timer;
QProgressBar *m_ProgressBar;
public:
WaitingDialog(QWidget *parent = 0);
~WaitingDialog();
void Start(int interval=100, int maxValue=100);
void Stop();
private slots:
void UpdateSlot();
};
WaitingDialog::WaitingDialog(QWidget *parent)
{
m_ProgressBar = new QProgressBar(this);
m_CurrentValue = m_MaxValue = m_UpdateInterval = 0;
m_ProgressBar->setRange(0, 100);
connect(&m_Timer, SIGNAL(timeout()), this, SLOT(UpdateSlot()));
m_ProgressBar->setTextVisible(false);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_ProgressBar);
setLayout(layout);
}
WaitingDialog::~WaitingDialog()
{
}
void WaitingDialog::UpdateSlot()
{
m_CurrentValue++;
if( m_CurrentValue == m_MaxValue )
m_CurrentValue = 0;
m_ProgressBar->setValue(m_CurrentValue);
}
void WaitingDialog::Start(int interval/* =100 */, int maxValue/* =100 */)
{
m_UpdateInterval = interval;
m_MaxValue = maxValue;
m_Timer.start(m_UpdateInterval);
m_ProgressBar->setRange(0, m_MaxValue);
m_ProgressBar->setValue(0);
}
void WaitingDialog::Stop()
{
m_Timer.stop();
}
#include "main.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
WaitingDialog *dialog = new WaitingDialog;
dialog->setWindowTitle("Please wait...");
QEventLoop *loop = new QEventLoop;
dialog->Start(50, 150)//没有红色局部事件循环代码会闪退
dialog->show();
//开启一个事件循环,10秒后退出
QTimer::singleShot(10000, loop, SLOT(quit()));
loop->exec();
return 0;
}
当程序在执行一项(或多项)耗时比较久的操作时,界面总要有一点东西告诉用户“程序还在运行中”,那么,一个“没有终点”的进度条就是你需要的了。
PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程,造成程序卡死的假象。
思路:程序很简单,一个进度条,一个定时器就足够了。
截图:
源代码:
[cpp] view
plaincopyprint?
#include <QtCore>
#include <QtGui>
class WaitingDialog : public QDialog
{
Q_OBJECT
private:
int m_CurrentValue; //当前值
int m_UpdateInterval; //更新间隔
int m_MaxValue; //最大值
QTimer m_Timer;
QProgressBar *m_ProgressBar;
public:
WaitingDialog(QWidget *parent = 0);
~WaitingDialog();
void Start(int interval=100, int maxValue=100);
void Stop();
private slots:
void UpdateSlot();
};
WaitingDialog::WaitingDialog(QWidget *parent)
{
m_ProgressBar = new QProgressBar(this);
m_CurrentValue = m_MaxValue = m_UpdateInterval = 0;
m_ProgressBar->setRange(0, 100);
connect(&m_Timer, SIGNAL(timeout()), this, SLOT(UpdateSlot()));
m_ProgressBar->setTextVisible(false);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_ProgressBar);
setLayout(layout);
}
WaitingDialog::~WaitingDialog()
{
}
void WaitingDialog::UpdateSlot()
{
m_CurrentValue++;
if( m_CurrentValue == m_MaxValue )
m_CurrentValue = 0;
m_ProgressBar->setValue(m_CurrentValue);
}
void WaitingDialog::Start(int interval/* =100 */, int maxValue/* =100 */)
{
m_UpdateInterval = interval;
m_MaxValue = maxValue;
m_Timer.start(m_UpdateInterval);
m_ProgressBar->setRange(0, m_MaxValue);
m_ProgressBar->setValue(0);
}
void WaitingDialog::Stop()
{
m_Timer.stop();
}
#include "main.moc"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
WaitingDialog *dialog = new WaitingDialog;
dialog->setWindowTitle("Please wait...");
QEventLoop *loop = new QEventLoop;
dialog->Start(50, 150)//没有红色局部事件循环代码会闪退
dialog->show();
//开启一个事件循环,10秒后退出
QTimer::singleShot(10000, loop, SLOT(quit()));
loop->exec();
return 0;
}
相关文章推荐
- Qt程序发布
- Qt安装与新建项目
- QT + Win7 SDK 中文时 乱码 常量中有换行符 问题
- 中标麒麟下编译Qt遇到的问题
- Qt 读取文件,获得文件名、路径
- QtWidgets继承关系树
- QtCore继承关系树
- 在win7旗舰版64位系统中,qtp破解密钥时,程序运行马上提示已停止工作
- EXTJS4中更改了ext:qtip的支持
- qt中如果用qDebug输出彩色调试信息
- There's no Qt version assigned to this project for platform Win32. Please use the 'change Qt version
- PyQt4百行代码自制密码管理器(三):数据库引入
- 一起看代码来玩玩QT之13 IO(two TCPServer TcpClient TcpScoket)
- Qt5.6.0+OpenGL 纹理贴图首战告捷
- QT与泰剧
- 用Qt实现一个桌面弹幕程序(七)-- -- 桌面客户端实现③
- Qt之QNetworkInterface
- Qt之QNetworkInterface
- Qt之QHostInfo
- Qt之QHostInfo