版权声明:请在征得作者同意的情况下,可以进行非盈利性引用。引用请注明出处:“作者:慕华思弦 转载地址” 字样,以尊重作者的劳动成果,并保持良好的版权意识。 https://blog.csdn.net/Superman___007/article/details/87284856
1.创建Qt继承自 QDialog 的项目.
2.直接贴代码吧,Qt5.7的环境,可以直接运行.
main.cpp的代码不做任何修改.
下面是 .h 文件的代码:
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QDialog>
namespace Ui {
class MyDialog;
}
class MyDialog : public QDialog
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private:
Ui::MyDialog *ui;
public:
void Traversal(QString); //对文件夹进行遍历获取文件名称
private slots:
void on_pushButton_clicked(); //三个按钮的槽
void on_pushButton_2_clicked();
void on_SelectBtn_clicked();
private:
int pathSize; //一个全局int变量
};
#endif // MYDIALOG_H
下面是 .cpp 文件的代码 .
#include "mydialog.h"
#include "ui_mydialog.h"
#include <QDir>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
}
MyDialog::~MyDialog()
{
delete ui;
}
void MyDialog::on_pushButton_clicked() //打开文件的按钮的方法
{
QString folder_path = QFileDialog::getExistingDirectory(this,"Select Folders","E:\\KqPlannigDeal_QT\\include"); //获得选择文件夹的路径(路径可自定义)
if(folder_path.isEmpty())
return;
else
ui->lineEdit->setText(folder_path); //获取编辑框的内容
}
void MyDialog::on_pushButton_2_clicked() //生成 .h 文件 到指定文件夹下的按钮的方法
{
QString path,path2;
path = ui->lineEdit->text(); //获取文本编辑框1的内容
path2 = ui->lineEdit_2->text(); //获取文本编辑框2的内容
pathSize = path.size();
if(path.isEmpty())
{
QMessageBox::warning(NULL,"warning","Please fill in the folder path!",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
return;
}
else if(path2.isEmpty())
{
QMessageBox::warning(NULL,"warning","Please fill in the path of the generated file!",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
return;
}
Traversal(path); //开始遍历指定文件夹下的 .h 文件
QMessageBox::information(NULL,"Tip","Successful creation of. h file",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes);
return;
}
void MyDialog::Traversal(QString path) //遍历文件夹里面的 .h 文件的方法
{
QDir dir(path);
QString RelativeRPath,ResultPath;
QString saveFile,saveFolder;
int index,FolderSize;
foreach (QFileInfo mfi, dir.entryInfoList())
{
if(mfi.isFile() && mfi.suffix()=="h") //判断文件不为空,后缀名为 .h 的文件(可自定义后缀实现查找指定后缀文件)
{
path = mfi.absoluteFilePath();
RelativeRPath = path.mid(pathSize + 1);
ResultPath = QString("#include \"%1\"").arg(RelativeRPath);
//qDebug() <<ResultPath;
saveFolder = ui->lineEdit->text();
index = saveFolder.lastIndexOf('/'); //查找最后一个 '/' 的位置
FolderSize = saveFolder.size();
saveFolder = saveFolder.right(FolderSize - index) + ".h"; //获取最后一个 '/' 后面的文件 加上 '.h'
//下面开始将获取的文件名写入到文件中
saveFile = ui->lineEdit_2->text() + saveFolder; //从编辑框2中获取保存的路径
QFile file(saveFile);
if(!file.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append))
QMessageBox::warning(this,"file write","can't open",QMessageBox::Yes);
QTextStream in(&file); //写入文件
in<<ResultPath<<"\n";
file.close();
}
else
{
if(mfi.fileName()=="." || mfi.fileName()=="..")
continue;
//qDebug() << "Entry Dir:" << mfi.absoluteFilePath();
Traversal(mfi.absoluteFilePath());
}
}
}
void MyDialog::on_SelectBtn_clicked() //选择保存文件的按钮的方法
{
QString folder_path = QFileDialog::getExistingDirectory(this,"Select Folders","C:\\Users\\Administrator\\Desktop\\hFile"); //路径可自定义
if(folder_path.isEmpty())
return;
else
ui->lineEdit_2->setText(folder_path);
}
下面是ui文件的控件的对象名 .
使用说明 : 1.点击"打开"选择需要遍历文件的文件夹,选择完后路径就显示到lineEdit上面了.
2.点击"选择"选择需要将遍历的所有文件按照指定格式进行保存到创建的文件中.
3.点击"生成.h文件"就生成.h文件到指定路径下了.
效果如下: