QT之:文件打开,文件目录打开,文件保存,字体颜色选择和字体的选择

#include "QtGuiApplication3.h"
#include"QDir"

#include"QFileDialog"
#include"QColorDialog"
#include"QFontDialog"


QtGuiApplication3::QtGuiApplication3(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	this->setWindowTitle(QStringLiteral("文本文件读写")); //设置窗体标题

	Imc_Total();
}

void QtGuiApplication3::Imc_Total()
{
	//////          打开一个文件
	QObject::connect(ui.pushButton, &QPushButton::pressed, [this]() {
		QString curPath = QDir::currentPath();  //获取当前应用程序的目录
		QString dlgTitle = "选择一个文件";      //
		QString filter = "程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*);;图片文件(*.jpg *.png *.tif *.gif)";  //文件过滤器(选择不同后缀的文件)
		QString aFileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter);  //返回带路径的完整文件名

		ui.lineEdit->setText(aFileName); //将打开文件地址显示在编辑框

		if (aFileName.isEmpty())
			return;
		
	});

	
	//////         选择打开多个文件


	QObject::connect(ui.pushButton_mul_files, &QPushButton::pressed, [this]() {
		QString curPath = QDir::currentPath();  //获取当前应用程序的目录
		QString dlgTitle = "选择多个文件";      //
		QString filter = "程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*);;图片文件(*.jpg *.png *.tif *.gif)";  //文件过滤器(选择不同后缀的文件)
		
		
		QStringList fliterList = QFileDialog::getOpenFileNames(this, dlgTitle, curPath, filter);  //返回带路径的完整文件名
		for (int i = 0;i < fliterList.count();i++)
		{
			ui.plainTextEdit->appendPlainText(fliterList.at(i));
		}
		if (fliterList.isEmpty())
			return;

	});


	//////      选择已有目录
	QObject::connect(ui.pushButton_getExitDirectpry, &QPushButton::pressed, [this]() {
	
		QString curPath = QCoreApplication::applicationDirPath();
		QString dlgTitle = "选择一个目录";
		QString selectDir = QFileDialog::getExistingDirectory(this,dlgTitle,curPath,QFileDialog::ShowDirsOnly);

		if (!selectDir.isEmpty())
		{
			ui.plainTextEdit->appendPlainText(selectDir);
		}

	});


	//////      选择保存文件名

	QObject::connect(ui.pushButton_getSaveFiles, &QPushButton::pressed, [this]() {
	
		QString curPath = QCoreApplication::applicationDirPath();
		QString dlgTitle = "保存文件";
		QString filter = "h文件(*.h);;c++文件(*.cpp);;所有文件(*.*)";

		QString aFileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);

		if (!aFileName.isEmpty())
		{
			ui.plainTextEdit->appendPlainText(aFileName);
		}
	
	});


//////// 选择颜色-----修改文本框字体颜色
	QObject::connect(ui.pushButton_setColor, &QPushButton::pressed, [this]() {
	
		QPalette pal = ui.plainTextEdit->palette();  //获取现有颜色
		QColor inintColor = pal.color(QPalette::Text); //现有文字颜色
		QColor color = QColorDialog::getColor(inintColor, this, "选择颜色");
		if (color.isValid())
		{
			pal.setColor(QPalette::Text, color);
			ui.plainTextEdit->setPalette(pal);
		}

	});

	
////////选择字体-----修改文本框文字的字体名称、大小、粗体、斜体等

	QObject::connect(ui.pushButton_setFont, &QPushButton::pressed, [this]() {
		QFont inintFont = ui.plainTextEdit->font();  //获取当前字体
		bool ok = false;

		QFont font = QFontDialog::getFont(&ok, inintFont);

		if (ok)
		{
			ui.plainTextEdit->setFont(font);
		}
	
	});


	///////////////退出界面
	QObject::connect(ui.pushButton_exit, &QPushButton::pressed, [this]() {
	
		QApplication *app;
		app->exit();
	
	});




}



 

你可能感兴趣的:(QT和C++)