我是靠谱客的博主 感性花瓣,最近开发中收集的这篇文章主要介绍Qt 旋转等待加载类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

//***************************************头文件*************************************
#ifndef KBDWAITPRGDLG_H
#define KBDWAITPRGDLG_H
#include <QDialog>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <QTimer>
#include <QMovie>
#include <QProgressDialog>
#include "DataType.h"
typedef boost::function<void()> ThreadFun;
class Q_DECL_EXPORT KbdWaitPrgDlg : public QDialog
{
Q_OBJECT
public:
KbdWaitPrgDlg(
ThreadFun fun,
QWidget *parent = 0);
~KbdWaitPrgDlg();
int exec();
//设置标题头
void setTitle(const QString& title);
//显示消息
void setShowMsg(const QString& msg);
//设置最小显示时间
void setMinimumDuration(int ms);
signals:
void thdFinish();
void thdStart();
protected:
virtual void closeEvent(QCloseEvent * ev);
private slots:
void slot_timeOut();
private:
void moveToCenter();
private:
int64 m_benginDuration;
int m_minimumDuration;
QString m_title;
QString m_showMsg;
boost::shared_ptr<boost::thread> m_ptrThread;
ThreadFun m_fun;
QTimer* m_timer;
};
#endif // KBDWAITPRGDLG_H
//***************************************实现文件*************************************
#include "kbdwaitprgdlg.h"
#include <QCloseEvent>
#include <QDesktopWidget>
#include <QApplication>
#include <QLabel>
#include <QGridLayout>
#include "pub_utility_api/TimeUtil.h"
#include <QMainWindow>
KbdWaitPrgDlg::KbdWaitPrgDlg(
ThreadFun fun,
QWidget *parent /*= 0*/)
: QDialog(parent),
m_fun(fun),
m_minimumDuration(200)
{
this->setWindowFlags(Qt::Dialog |Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(slot_timeOut()));
QWidget* bkWidget = new QWidget(this);
QGridLayout *layout = new QGridLayout(bkWidget);
QLabel *lable = new QLabel(bkWidget);
lable->setMaximumSize(QSize(80, 80));
QMovie *movie = new QMovie(this);
movie->setFileName(":/res/load.gif");
lable->setMovie(movie);
layout->addWidget(lable);
this->setLayout(layout);
movie->start();
this->layout()->setEnabled(false);
this->resize(1,1);
}
KbdWaitPrgDlg::~KbdWaitPrgDlg()
{
if (m_ptrThread)
{
m_ptrThread->join();
m_ptrThread.reset();
}
if (m_timer)
{
delete m_timer;
m_timer = NULL;
}
}
int KbdWaitPrgDlg::exec()
{
if (m_ptrThread)
{
return QDialog::Rejected;
}
m_benginDuration = kbd_public::getUTCTimeMsec();
m_timer->start(100);
emit thdStart();
m_ptrThread = boost::make_shared<boost::thread>(m_fun);
this->layout()->setEnabled(false);
this->resize(1, 1);
return QDialog::exec();
}
void KbdWaitPrgDlg::setTitle(const QString& title)
{
m_title = title;
}
void KbdWaitPrgDlg::setShowMsg(const QString& msg)
{
m_showMsg = msg;
}
void KbdWaitPrgDlg::setMinimumDuration(int ms)
{
m_minimumDuration = ms;
}
void KbdWaitPrgDlg::closeEvent(QCloseEvent * ev)
{
if (m_ptrThread)
{
m_ptrThread->join();
m_ptrThread.reset();
}
QDialog::closeEvent(ev);
}
void KbdWaitPrgDlg::slot_timeOut()
{
if (!m_timer)
{
return;
}
if (!m_ptrThread)
{
this->accept();
return;
}
if (m_ptrThread->timed_join(0))
{
emit thdFinish();
this->accept();
m_ptrThread.reset();
}
int64 nowTime = kbd_public::getUTCTimeMsec();
if (nowTime - m_benginDuration > m_minimumDuration)
{
this->layout()->setEnabled(true);
this->resize(200, 200);
moveToCenter();
}
}
void KbdWaitPrgDlg::moveToCenter()
{
QWidgetList deskList = QApplication::topLevelWidgets();
QWidget* desk = NULL;
QWidgetList::iterator iter = deskList.begin();
for (; iter!=deskList.end(); ++iter)
{
if (qobject_cast<QMainWindow*>(*iter))
{
desk = *iter;
}
}
if (desk && (!desk->isHidden()))
{
int wd = desk->width();
int hd = desk->height();
QPoint movePos((wd - this->width()) / 2,
(hd - this->height()) / 2);
this->move(desk->mapToGlobal(movePos));
}
else
{
desk = QApplication::desktop();
if (!desk)
{
return;
}
int wd = desk->width();
int hd = desk->height();
QPoint movePos((wd - this->width()) / 2,
(hd - this->height()) / 2);
this->move(movePos);
}
}

 

最后

以上就是感性花瓣为你收集整理的Qt 旋转等待加载类的全部内容,希望文章能够帮你解决Qt 旋转等待加载类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部