-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwfileinfo.cpp
54 lines (48 loc) · 1.32 KB
/
wfileinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "wfileinfo.h"
WFileInfo::WFileInfo(QWidget *parent) : QWidget(parent)
{
}
void WFileInfo::openFile(){
QFileDialog *fileDialog = new QFileDialog;
QString filePath = fileDialog->getOpenFileName(this, tr("选择文件"), ".", tr("文体文件(*.txt)"));
if (0 == filePath.length()){
return;
} else {
this->filePath = filePath;
}
}
QString WFileInfo::getFilePath(){
return this->filePath;
}
QString WFileInfo::getFileContents(QString filePath){
QString fileName;
if (filePath.length() > 0){
fileName = filePath;
} else {
fileName = this->filePath;
}
QTextCodec * code = QTextCodec::codecForName("utf8");
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this,"Warning","can't open",QMessageBox::Yes);
}
QTextStream stream(&file);
stream.setCodec(code);//对输出流的设置
while (stream.atEnd() == 0) {
this->fileContents = stream.readAll();
}
file.close();
return this->fileContents;
}
QString WFileInfo::getFileName(QString filePath)
{
QString fileName;
if (filePath.length() > 0){
fileName = filePath;
} else {
fileName = this->filePath;
}
QFileInfo *fileInfo = new QFileInfo(fileName);
return fileInfo->baseName();
}