主要应用的函数
QtMsgHandler qInstallMsgHandler(QtMsgHandler);
其中QtMsgHandler是函数指针,原型为
typedef void (*QtMsgHandler)(QtMsgType, const char *);
以下是在代码中的简单应用(以QNetWorkAccessManager 下载文件为例):
头文件实现:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QFile>
//日志处理类
class logger: public QObject
{
Q_OBJECT
public:
~logger(){}
static logger *_instance;
static logger *instance();
public:
void loggerMaster(const QString &);
signals:
void G_sndMsg(const QString &);
private:
//将默认构造函数设置为私有
logger(QObject *parent = 0):
QObject(parent){}
};
//窗口类
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void S_disLog(const QString &);
void S_updateProgress(qint64 _done, qint64 _total);
void S_getZipData();
void S_finishDown();
private:
Ui::MainWindow *ui;
QNetworkAccessManager *manager;
QNetworkReply *reply;
bool isDownLoadOver;
QFile file;
};
#endif // MAINWINDOW_H
cpp文件实现
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QDebug>
//-----------------------------------------日志处理部分开始
//初始化静态变量
logger * logger::_instance = 0;
//构建单实例日志对象
logger * logger::instance()
{
if(!logger::_instance)
logger::_instance = new logger;
return logger::_instance;
}
void logger ::loggerMaster(const QString & msg)
{
//在日志信息中加入时间标记
QString newLog = QDateTime::currentDateTime().toString(QLatin1String("MM-dd hh:mm:ss:zzz"))
+ QLatin1Char(' ') + msg;
//发送处理后的日志信息
emit G_sndMsg(newLog);
}
//日志处理函数
void logCatcher(QtMsgType type,const char* msg)
{
if(type == QtDebugMsg || type == QtWarningMsg)
//将日志信息传递给logger处理函数
logger::instance()->loggerMaster(QString::fromLocal8Bit(msg));
}
//------------------------------------------日志处理部分结束
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
isDownLoadOver(false),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//构建QNetworkAccessManager对象
manager = new QNetworkAccessManager(this);
//准备下载文件的地址:例如htp:htp://***.***.***.***/ze.zip
QUrl url("ht*********-win32-2.0.1.zip");
QNetworkRequest request(url);
reply = manager->get(request);
//更新现在进度条
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),SLOT(S_updateProgress(qint64,qint64)));
//接收数据
connect(reply,SIGNAL(readyRead()),SLOT(S_getZipData()));
//提示接收数据完成
connect(reply,SIGNAL(finished()),SLOT(S_finishDown()));
//------------------------------------日志处理
//注册日志处理函数
qInstallMsgHandler(logCatcher);
//连接日志,接收从logger实例中返回的日志信息
connect(logger::instance(),SIGNAL(G_sndMsg(QString)),SLOT(S_disLog(QString)));
file.setFileName("download.zip");
//打开文件
file.open(QIODevice::WriteOnly);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
close();
}
//将日志信息追加到QPlainTextEdit控件中
void MainWindow::S_disLog(const QString & msg)
{
ui->plainTextEdit->appendPlainText(msg);
}
//更新进度条
void MainWindow::S_updateProgress(qint64 _done, qint64 _total)
{
int qVal = qRound(_done/(double)_total * 100);
if( qVal > 100 ) qVal = 100;
ui->progressBar->setValue(qVal);
qDebug()<<QString("已经下载文件的 \%%1").arg(QString::number(qVal,10));
if(100 == qVal)
{
isDownLoadOver = true;
}
}
//获取数据
void MainWindow::S_getZipData(/*QNetworkReply *_relay*/)
{
if( reply->error() != QNetworkReply::NoError ) {
qWarning() << tr("...文件下载失败...")+ reply->errorString();
file.remove();
ui->pushButton->setEnabled(true);
return;
}
QByteArray bArray = reply->readAll();
file.write(bArray);
}
//下载完成
void MainWindow::S_finishDown()
{
if(isDownLoadOver)
{
qDebug() << (tr("...软件下载成功..."));
ui->pushButton->setEnabled(true);
}
}
实现效果:
//以上我在做软件更新的时候用到过。