项目效果:
修改界面后的运行效果:
项目文件列表:
界面:
修改后的界面:
修改说明:将上面的三个控件依次选中,然后将三者的布局设置为水平格式,这个操作在工具中的界面界面编辑器中完成设置。
项目程序:
mywidget.h:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QStandardItemModel>
#include <QMenu>
#include <QAction>
#include <QTableWidget>
//#include "settings.h"
namespace Ui {
class MyWidget;
}
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);//声明为explicit的构造函数不能在隐式转换中使用。
~MyWidget();//析构函数,用于释放创建的对象
private slots:
void on_pb_SelectFile_clicked();//选择文件
void on_pb_SaveFile_clicked();
private:
Ui::MyWidget *ui;
// Settings *m_pSettings;//读写工具类对象
QString FilePath;//记录文件的路径
QMenu *menu; //菜单对象
bool OpenSeleteFile();//弹出选择文件对话框
void ShowTxtToWindow();//显示txt文件内容
// void SaveTxtDocument();//保存内容至文件
};
#endif // MYWIDGET_H
main.cpp:
#include <QtGui/QApplication>
#include "mywidget.h"
//#include "myinifile.h"
#include <QObject>
#include <QTextCodec>
void SetCodec(QApplication &app)
{
QTextCodec *codec = QTextCodec::codecForName("UTF-8");//设置编码格式为UTF-8
QTextCodec::setCodecForTr(codec);//调整QT字符,使非英文字符可用,这个函数的作用是设置传给tr函数时的默认字串编码
QTextCodec::setCodecForLocale(codec);//这个函数主要用于设置和对本地文件系统读写时候的默认编码格式。
QTextCodec::setCodecForCStrings(codec);//这个函数主要用在用字符常量或者QByteArray构造QString对象时使用的默认编码方式
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SetCodec(a);
MyWidget w;
w.setWindowTitle(QObject::tr("文件读写工具 "));
w.show();
return a.exec();
}
mywidget.cpp:
#include "mywidget.h"
#include "ui_mywidget.h"
#include <QMessageBox>
#include <QStandardItemModel>
#include <QtableWidget>
#include <QListWidget>
#include <QDebug>
#include <QVariant>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QItemSelectionModel>
#include <QModelIndexList>
#include <QModelIndex>
#include <QHeaderView>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyWidget)
{
ui->setupUi(this);
}
/**
* @brief MyWidget::~MyWidget
* 析构函数:用来释放
*/
MyWidget::~MyWidget()
{
delete ui;
}
void MyWidget::on_pb_SelectFile_clicked()//选择文件功能函数
{
if(false == OpenSeleteFile())//弹出选择文件对话框 如果成功选择文件,主线程myWidget类就有对象存储了文件路径
{
return;
}
if(FilePath == "")//如果没有选择文件,即文件路径为空
{
return;
}
ui->le_FilePath->setText(FilePath);//如果选中了文件,setText() 或者 insert() 改变其中的文本,那么行编辑器中就会出现选中的文件的路径
ShowTxtToWindow();
}
void MyWidget::on_pb_SaveFile_clicked()//保存文件功能函数
{
QFile myfile(FilePath);//创建一个输出文件的文档
if (myfile.open(QFile::WriteOnly|QFile::Text))//注意WriteOnly是往文本中写入的时候用,ReadOnly是在读文本中内容的时候用,Truncate表示将原来文件中的内容清空
{
QTextStream out(&myfile);
out << ui->textEdit->toPlainText();
}
ShowTxtToWindow();
}
bool MyWidget::OpenSeleteFile()//弹出选择文件对话框
{
/**
* @brief strPath
* 类名::函数名->此时的函数是一个静态函数。例如:getOpenFileName
* getOpenFileName()函数提供了六个参数:
* * 第一个参数的作用是用于指定父组件,基本上构造函数都会提供一个默认的值,即parent = 0;
* * 第二个参数的作用是显示的按钮的标题
* * 第三个参数的作用是对话框显示时默认打开的目录
* * 第四个参数的作用是对话框的后缀名过滤器,过滤掉不需要选择的文件名称
* * 第五个参数的作用是默认选择的过滤器
* * 第六个参数的作用是对话框的一些参数设定,例如对话框只显示文件夹等信息
* ** 注意:还函数有的参数这是可以不需要选择的,例如最后两个参数都是可以省略的,
* 但是第一个参数和第三个参数当没有特殊要求的时候,将第一个参数置为NULL,
* 第三个参数置为空字符串。即"";
*/
QString strPath = QFileDialog::getOpenFileName(NULL,QString::fromUtf8("选择文件"),"",QObject::tr("txt(*.txt)"));
if(strPath == "")
{
QMessageBox::information(this,QString::fromUtf8("提示"),QString::fromUtf8("选择文件失败,无路径"),"OK");
return false;//用户点击的取消按钮
}
FilePath = strPath;
return true;
}
void MyWidget::ShowTxtToWindow()//显示文本文件中的内容
{
QString fileName = FilePath;
if(!fileName.isEmpty())
{
QFile *file = new QFile;
file->setFileName(fileName);
bool ok = file->open(QIODevice::ReadOnly);
if(ok)
{
QTextStream in(file);
ui->textEdit->setText(in.readAll());
file->close();
delete file;
}
else
{
QMessageBox::information(this,"错误信息","打开文件:" + file->errorString());
return;
}
}
}