//LogWidget.h (mainwindow.h)
#ifndef LogWidget_H
#define LogWidget_H
#include <QMainWindow>
#include <QDebug>
#include <QFile>
namespace Ui {
class LogWidget;
}
class LogWidget : public QMainWindow
{
Q_OBJECT
public:
explicit LogWidget(QWidget *parent = 0);
~LogWidget();
private slots: //槽函数
void on_pushButton_clicked();
private:
Ui::LogWidget *ui;
};
#endif // LogWidget_H
//LogWidget.cpp (mainwindow.cpp)
#include "LogWidget.h"
#include "ui_LogWidget.h" //uic工具自动由xx.ui生成ui_xxx.h
//构造函数
LogWidget::LogWidget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LogWidget)
{
ui->setupUi(this);
}
//析构函数
LogWidget::~LogWidget()
{
delete ui;
}
void LogWidget::on_pushButton_clicked()
{
QString displayString;
QFile file("C:\\Users\\zwc11\\Yeecoh\\log.txt"); //目标文件路径
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"Can't open the file!";
}
while(!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
qDebug()<< str;
displayString.append(str);
}
ui->textEdit->clear();
ui->textEdit->setPlainText(displayString);
}
//LogWidget.ui (xml格式) (mainwindow.ui)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LogWidget</class>
<widget class="QMainWindow" name="LogWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1179</width>
<height>640</height>
</rect>
</property>
<property name="windowTitle">
<string>LogWidget</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<width>1151</width>
<height>491</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>390</x>
<y>20</y>
<width>301</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>读取日志</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1179</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
// fileRead.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = fileRead //.pro文件名
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
LogWidget.cpp
HEADERS += LogWidget.h
FORMS += LogWidget.ui
//main.cpp
#include "LogWidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LogWidget w;
w.show();
return a.exec();
}
//LogWidget.h
#ifndef LogWidget_H
#define LogWidget_H
#include <QDialog>
#include <QDebug>
#include <QFile>
namespace Ui {
class LogWidget;
}
class LogWidget : public QDialog //区别只有这里换成继承自QDialog类
{
Q_OBJECT
public:
explicit LogWidget(QWidget *parent = 0);
~LogWidget();
private slots:
void on_pushButton_clicked();
private:
Ui::LogWidget *ui;
};
#endif // LogWidget_H
//LogWidget.cpp
#include "LogWidget.h"
#include "ui_LogWidget.h"
LogWidget::LogWidget(QWidget *parent) :
QDialog(parent), //区别就是这里换成QDialog
ui(new Ui::LogWidget)
{
ui->setupUi(this);
}
LogWidget::~LogWidget()
{
delete ui;
}
void LogWidget::on_pushButton_clicked()
{
QString displayString;
QFile file("C:\\Users\\zwc11\\Yeecoh\\log.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
//qDebug()<<"Can't open the file!"; //如果qDebug()内容一边写入log.txt,一边读取到文件末尾,则会死循环不停地写入
} //日志文件会在几秒里扩充到上百MB
while(!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
//qDebug()<< str;
displayString.append(str);
}
ui->textEdit->clear();
ui->textEdit->setPlainText(displayString);
}
//LogWidget.ui:与上文完全相同
//YeecohWindow.cpp (mainwindow.cpp)
void YeecohWindow::log()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("打开详细日志"), //打开文件窗口名称
"C:\\Users\\zwc11\\Yeecoh", //默认搜索路径
tr("txt(*.txt);;All files(*.*)")); //过滤器
if (fileName.isEmpty()) {
QMessageBox::warning(this, "Warning!", "Failed to open the log.txt ! ");
}
else{ //重点设计一下这里
LogWidget win;
win.exec();
}
}