Qt视频直播软件--项目实战(Day2)
生活随笔
收集整理的這篇文章主要介紹了
Qt视频直播软件--项目实战(Day2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第二天項目日記
1、今天總結
今天繼續寫服務器
1)主要實現登錄日志的功能
2)創建一個寫日志的類,學會使用Qt的文件操作
3)學會使用QListView的使用
4)實現日志實時顯示和計入文件
5)每天一個日志文件,避免單個日志文件過大
2、設計思路
為記錄日志寫一個單獨的c++類,并且使用單例,避免參數的傳遞
點擊日志按鈕彈出一個widget界面,為了避免重復點擊出現多個widget也使用單例
要實現實時刷新日志,就需要在寫日志時給日志界面發信號
每天要將日志寫入不同的文件中
3、代碼說明
新創建的文件有五個,用紅色框出來的
mylog.h (繼承自QObject 單例模式)
#ifndef MYLOG_H #define MYLOG_H#include <QObject> #include <QDir> #include <QDebug> #include <QDateTime> #include <QFile>class MyLog : public QObject {Q_OBJECT private:explicit MyLog(QObject *parent = 0,QString Path = 0);static MyLog *instance;QString path; //路徑 運行路徑創建logQString filename; //寫日志的文件 log/日期.logQString todaytime; //獲取今天的日期public:static MyLog *getInstance(QString); //單例模式void LogWrite(QString str);//寫日志函數,格式可以自己定義QString getpath();signals:void fileflush();//日志更新信號,用來給日志文件發送信號 public slots: };#endif // MYLOG_Hmylog.cpp
#include "mylog.h"MyLog::MyLog(QObject *parent, QString Path):QObject(parent) {path = Path+'/'+"log";//創建QDir指針QDir *folder = new QDir;//判斷文件夾是否存在bool exist = folder->exists(path);if(exist){// qDebug()<<Path<<" is exist";}else{//創建文件夾bool ok = folder->mkdir(path);if(ok){// qDebug()<<path<<" create success";}else{// qDebug()<<path<<" create failed";}}//獲取日期QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("yyyyMMdd");//創建文件//存入參數todaytime = current_date;filename = path + '/' + current_date + ".log"; }MyLog *MyLog::getInstance(QString Path) {//調用的時候先判斷一下當前時間QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("yyyyMMdd");//只有第一次創建時候path有用所以只需要保證第一次調用時候傳入path就好了if(instance == NULL){instance = new MyLog(NULL,Path);}if(instance->todaytime != current_date){//如果已經跨天了,就更新一下file nameinstance->todaytime = current_date;instance->filename = instance->path + '/' + current_date + ".log";}return instance; }void MyLog::LogWrite(QString str) {//自定義日志格式QFile myfile(filename);QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("[yyyy.MM.dd hh:mm:ss.zzz]");QString logbuf = current_date + " "+str+"\n";myfile.open(QIODevice::Append | QIODevice::Text);QByteArray mystr = logbuf.toUtf8();myfile.write(mystr);myfile.close();//用來刷新日志讀取emit fileflush(); }QString MyLog::getpath() {return path; }logwidget.h (繼承自QWidget 單例模式)
#ifndef LOGWIDGET_H #define LOGWIDGET_H#include <QWidget> #include "mylog.h" #include <QStringListModel> #include <QModelIndex> #include <QDebug> #include <QSettings> #include <QMessageBox> #include <QDateTime> #include <QCloseEvent>namespace Ui { class LogWidget; }class LogWidget : public QWidget {Q_OBJECT private:explicit LogWidget(QWidget *parent = 0);static LogWidget *instance;public:static LogWidget *getInstance(); //單例模式~LogWidget();void updateLogList();QStringList getFileNames(const QString &path);void myReadFile(QString filename);private slots:void on_pushflush_clicked();void show_log();void update_log_show();private:Ui::LogWidget *ui;QString path;MyLog *mylog;QStringListModel *model;QString show_data;//上次雙擊之后選擇的日志文件protected:virtual void closeEvent(QCloseEvent *ev); };#endif // LOGWIDGET_Hlogwidget.cpp
#include "logwidget.h" #include "ui_logwidget.h"LogWidget::LogWidget(QWidget *parent) :QWidget(parent),ui(new Ui::LogWidget) {ui->setupUi(this);show_data = " ";this->setWindowTitle("日志信息");mylog = MyLog::getInstance(NULL);path = mylog->getpath();ui->listfiles->setEditTriggers(QAbstractItemView::NoEditTriggers);ui->textEdit->setText("雙擊列表顯示日志");ui->textEdit->setReadOnly(true);connect(ui->listfiles,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(show_log()));connect(mylog,SIGNAL(fileflush()),this,SLOT(update_log_show()));updateLogList(); }LogWidget *LogWidget::getInstance() {if(instance == NULL){instance = new LogWidget(NULL);}return instance; }LogWidget::~LogWidget() {delete ui; }void LogWidget::updateLogList() {ui->listfiles->clearMask();QStringList files = getFileNames(path);for(int i = 0;i< files.count();i++){qDebug()<<files.value(i)<<" ";}qDebug()<<"\n";//這里會不會有內存泄露model = new QStringListModel(files);ui->listfiles->setModel(model); }QStringList LogWidget::getFileNames(const QString &path) {QDir dir(path);QStringList nameFilters;nameFilters << "*.log";QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);return files; }void LogWidget::myReadFile(QString filename) {ui->textEdit->clear();QFile f(filename);if (!f.open(QIODevice::ReadOnly|QIODevice::Text))//打開指定文件{QMessageBox::about(NULL, "文件", "文件打開失敗");return;}while (!f.atEnd()){QString str = f.readLine();ui->textEdit->append(str);}f.close(); }void LogWidget::on_pushflush_clicked() {updateLogList(); }void LogWidget::show_log() {int row = ui->listfiles->currentIndex().row();QModelIndex index = model->index(row,0);QString name = model->data(index,0).toString();show_data = name.left(8);QString filename = path+'/'+ name;qDebug()<<filename;myReadFile(filename); }void LogWidget::update_log_show() {QDateTime current_date_time =QDateTime::currentDateTime();QString current_date =current_date_time.toString("yyyyMMdd");if(show_data == current_date){QString filename = path+'/'+ show_data + ".log";myReadFile(filename);} }void LogWidget::closeEvent(QCloseEvent *ev) {Q_UNUSED(ev);this->show_data = " ";ui->textEdit->setText("雙擊列表顯示日志"); }logwidget.ui
其他
main.cpp添加
MyLog * MyLog::instance = NULL; LogWidget* LogWidget::instance = NULL;
mainwindow.h 添加
mainwindow.cpp 添加
tcpserver.h
添加頭文件,并且調用單例的日志記錄接口
tcpserver.cpp
4、項目文件
源代碼鏈接
5、效果展示
6、每日總結
學會使用的模式:單例模式
學會使用的控件: QListView QTextEdit
學會使用的Qt庫:QDateTime QModelIndex QFile QDir QStringListModel QModelIndex
千里執行,始于足下!!!!!!
總結
以上是生活随笔為你收集整理的Qt视频直播软件--项目实战(Day2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt简单实现UDP通信
- 下一篇: 【密码学】抽象代数——群(学习笔记)