主要应用的函数
QtMsgHandler qInstallMsgHandler(QtMsgHandler);
其中QtMsgHandler是函数指针,原型为
typedef void (*QtMsgHandler)(QtMsgType, const char *);
以下是在代码中的简单应用:
头文件实现:
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <QMainWindow>
5 #include <QtNetwork/QNetworkAccessManager>
6 #include <QtNetwork/QNetworkReply>
7 #include <QFile>
8
9 //日志处理类
10 class logger: public QObject
11 {
12 Q_OBJECT
13 public:
14 ~logger(){}
15 static logger *_instance;
16 static logger *instance();
17
18 public:
19 void loggerMaster(const QString &);
20
21 signals:
22 void G_sndMsg(const QString &);
23
24 private:
25 //将默认构造函数设置为私有
26 logger(QObject *parent = 0):
27 QObject(parent){}
28 };
29
30
31 //窗口类
32 namespace Ui {
33 class MainWindow;
34 }
35
36 class MainWindow : public QMainWindow
37 {
38 Q_OBJECT
39
40 public:
41 explicit MainWindow(QWidget *parent = 0);
42 ~MainWindow();
43
44 private slots:
45 void on_pushButton_clicked();
46 void S_disLog(const QString &);
47 void S_updateProgress(qint64 _done, qint64 _total);
48 void S_getZipData();
49 void S_finishDown();
50
51 private:
52 Ui::MainWindow *ui;
53 QNetworkAccessManager *manager;
54 QNetworkReply *reply;
55 bool isDownLoadOver;
56 QFile file;
57 };
58
59 #endif // MAINWINDOW_H
cpp文件实现
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <QTime>
4 #include <QDebug>
5
6 //-----------------------------------------日志处理部分开始
7
8 //初始化静态变量
9 logger * logger::_instance = 0;
10
11 //构建单实例日志对象
12 logger * logger::instance()
13 {
14 if(!logger::_instance)
15 logger::_instance = new logger;
16 return logger::_instance;
17 }
18
19
20 void logger ::loggerMaster(const QString & msg)
21 {
22 //在日志信息中加入时间标记
23 QString newLog = QDateTime::currentDateTime().toString(QLatin1String("MM-dd hh:mm:ss:zzz"))
24 + QLatin1Char(' ') + msg;
25 //发送处理后的日志信息
26 emit G_sndMsg(newLog);
27 }
28
29 //日志处理函数
30 void logCatcher(QtMsgType type,const char* msg)
31 {
32 if(type == QtDebugMsg || type == QtWarningMsg)
33 //将日志信息传递给logger处理函数
34 logger::instance()->loggerMaster(QString::fromLocal8Bit(msg));
35 }
36
37
38 //------------------------------------------日志处理部分结束
39
40
41
42 MainWindow::MainWindow(QWidget *parent) :
43 QMainWindow(parent),
44 isDownLoadOver(false),
45 ui(new Ui::MainWindow)
46 {
47 ui->setupUi(this);
48
49 //构建QNetworkAccessManager对象
50 manager = new QNetworkAccessManager(this);
51 //准备下载文件的地址:例如htp:htp://***.***.***.***/ze.zip
52 QUrl url("ht*********-win32-2.0.1.zip");
53 QNetworkRequest request(url);
54 reply = manager->get(request);
55
56
57 //更新现在进度条
58 connect(reply,SIGNAL(downloadProgress(qint64,qint64)),SLOT(S_updateProgress(qint64,qint64)));
59 //接收数据
60 connect(reply,SIGNAL(readyRead()),SLOT(S_getZipData()));
61 //提示接收数据完成
62 connect(reply,SIGNAL(finished()),SLOT(S_finishDown()));
63
64
65
66 //------------------------------------日志处理
67 //注册日志处理函数
68 qInstallMsgHandler(logCatcher);
69 //连接日志,接收从logger实例中返回的日志信息
70 connect(logger::instance(),SIGNAL(G_sndMsg(QString)),SLOT(S_disLog(QString)));
71
72 file.setFileName("download.zip");
73 //打开文件
74 file.open(QIODevice::WriteOnly);
75 }
76
77 MainWindow::~MainWindow()
78 {
79 delete ui;
80 }
81
82 void MainWindow::on_pushButton_clicked()
83 {
84 close();
85 }
86
87 //将日志信息追加到QPlainTextEdit控件中
88 void MainWindow::S_disLog(const QString & msg)
89 {
90 ui->plainTextEdit->appendPlainText(msg);
91 }
92
93
94 //更新进度条
95 void MainWindow::S_updateProgress(qint64 _done, qint64 _total)
96 {
97 int qVal = qRound(_done/(double)_total * 100);
98 if( qVal > 100 ) qVal = 100;
99 ui->progressBar->setValue(qVal);
100
101 qDebug()<<QString("已经下载文件的 \%%1").arg(QString::number(qVal,10));
102 if(100 == qVal)
103 {
104 isDownLoadOver = true;
105 }
106 }
107
108 //获取数据
109 void MainWindow::S_getZipData(/*QNetworkReply *_relay*/)
110 {
111 if( reply->error() != QNetworkReply::NoError ) {
112 qWarning() << tr("...文件下载失败...")+ reply->errorString();
113 file.remove();
114 ui->pushButton->setEnabled(true);
115 return;
116 }
117
118 QByteArray bArray = reply->readAll();
119
120 file.write(bArray);
121 }
122
123 //下载完成
124 void MainWindow::S_finishDown()
125 {
126 if(isDownLoadOver)
127 {
128 qDebug() << (tr("...软件下载成功..."));
129 ui->pushButton->setEnabled(true);
130 }
131 }
实现效果: