#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QString>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButtonBrowseSrc_clicked();
void on_pushButton_Load_clicked();
void on_pushButton_BrowseDst_clicked();
void on_pushButton_Save_clicked();
private:
Ui::Widget *ui;
void AnalyzeOneLine(const QByteArray &baLine);
//分析一行配置文本,设置到对应的控件里
};
#endif // WIDGET_H
#include "widget.h"标签:Widget,return,Qt,baTemp,tr,从文件,ui,lineEdit,读取 From: https://blog.51cto.com/u_15515702/5785672
#include "ui_widget.h"
#include <QRegExp>
#include <QRegExpValidator>
#include <QIntValidator>
#include <QFileDialog>
#include <QString>
#include <QFile>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//定义ipv4正则表达式,
QRegExp re("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
//新建正则表达式验证器
QRegExpValidator *revail=new QRegExpValidator(re);
//设置给lineEditIp
ui->lineEdit_Ip->setValidator(revail);
//设置一个整数验证器
QIntValidator *intVali=new QIntValidator(0,65535);
//设置给lineEditport
ui->lineEdit_Port->setValidator(intVali);
}
void Widget::on_pushButtonBrowseSrc_clicked()
{
QString strSrcName=QFileDialog::getOpenFileName(
this,
tr("打开配置文件"),
tr("."),
tr("Text files(*.txt);;All files(*)")
);
//该函数组关键的就是QFileDialog::getopenFileName()静态函数
//一般只要设置该静态函数的四个参数
//第一个参数是父窗口指针,第二个是弹出对话框的标题
//第三个是弹出对话框默认的开始路径,第四个是文件扩展名过滤字符串
//多个过滤器用分号隔开,扩展名放在圆括号内
if(strSrcName.isEmpty())
{
return ;
//空字符串不处理直接返回
}
else
{
ui->lineEdit_SrcFile->setText(strSrcName);
}
}
void Widget::on_pushButton_Load_clicked()
{
//获取源文件名
QString strSrc=ui->lineEdit_SrcFile->text();
if(strSrc.isEmpty())
{
return;
}
QFile fileIn(strSrc);
if(!fileIn.open(QIODevice::ReadOnly))
{
//打开错误
QMessageBox::warning(this,tr("打开错误"),
tr("打开文件错误")+fileIn.errorString());
return ;
}
while(!fileIn.atEnd())
{
QByteArray baLine=fileIn.readLine();
baLine=baLine.trimmed();
//剔除字符串两端空白
if(baLine.startsWith('#'))
{
continue;//不处理注释行
}
AnalyzeOneLine(baLine);
//正常的设置行,分析这一行的配置项
}
QMessageBox::information(this,tr("加载配置"),tr("加载配置完毕!"));
}
void Widget::on_pushButton_BrowseDst_clicked()
{
QString strDstName=QFileDialog::getSaveFileName(
this,
tr("保存配置文件"),
tr("."),
tr("TEXT files(*.txt);;ALL files(*)")
);
if(strDstName.isEmpty())
{
//空字符串不处理
return;
}
else
{
//设置要保存的文件名
ui->lineEdit_DstFile->setText(strDstName);
}
}
void Widget::AnalyzeOneLine(const QByteArray &baLine)
{
//按照等号分割
QList<QByteArray> list =baLine.split('=');
if(list.count() < 2)
{
return ;
//分隔之后没有配置值,无法设置
}
QByteArray optionName=list[0].trimmed().toLower();
QByteArray optionValue=list[1].trimmed();
QString strValue = QString::fromLocal8Bit( optionValue );
if("ip"==optionName)
{
ui->lineEdit_Ip->setText(strValue);
return ;
}
if("port"==optionName)
{
ui->lineEdit_Port->setText(strValue);
return ;
}
if("hostname"==optionName)
{
ui->lineEdit_HostName->setText(strValue);
return ;
}
if("workgroup"==optionName)
{
ui->lineEdit_WorkGroup->setText(strValue);
return ;
}
}
void Widget::on_pushButton_Save_clicked()
{
//获取保存文件名
QString strSaveName = ui->lineEdit_DstFile->text();
//获取设置值
QString strIP = ui->lineEdit_Ip->text();
QString strPort = ui->lineEdit_Port->text();
QString strHostName = ui->lineEdit_HostName->text();
QString strWorkGroup = ui->lineEdit_WorkGroup->text();
//如果字符串有空串就不写入
if( strSaveName.isEmpty() || strIP.isEmpty() || strPort.isEmpty()
|| strHostName.isEmpty() || strWorkGroup.isEmpty() )
{
QMessageBox::warning(this, tr("保存配置"),
tr("需要设置好保存文件名和所有配置项数值。"));
return;
}
//定义文件对象
QFile fileOut(strSaveName);
//打开
if( ! fileOut.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
QMessageBox::warning(this, tr("打开文件"),
tr("打开目的文件失败:") + fileOut.errorString() );
return;
}
//构造字节数组写入
//ip 行
QByteArray baTemp = "ip = ";
baTemp += strIP.toLocal8Bit() + "\n";
fileOut.write( baTemp );
//port 行
baTemp = "port = ";
baTemp += strPort.toLocal8Bit() + "\n";
fileOut.write( baTemp );
//hostname 行
baTemp = "hostname = ";
baTemp += strHostName.toLocal8Bit() + "\n";
fileOut.write( baTemp );
//workgroup 行
baTemp = "workgroup = ";
baTemp += strWorkGroup.toLocal8Bit() + "\n";
fileOut.write( baTemp );
//提示保存成功
QMessageBox::information(this, tr("保存配置"), tr("保存配置项成功!"));
}
Widget::~Widget()
{
delete ui;
}