#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
//字体按钮对应的槽函数
void Widget::on_fontBtn_clicked()
{
bool ok; //返回是否选中字体
//
//调出系统字体对话框
QFont f = QFontDialog::getFont(&ok,QFont("隶书",10,5,true),this,"字体");
//功能:调出系统字体对话框
//参数1:返回选中字体状态
//参数2:初始字体
//参数3:父组件
//参数4:对话框标题
//将选中的字体设置到文本编辑器中
if(ok)
{
//ui->msgEdit->setFont(f);
ui->msgEdit->setCurrentFont(f);
}
}
//颜色按钮对应的槽函数
void Widget::on_colorBtn_clicked()
{
QColor c = QColorDialog::getColor(QColor(35,203,190),this,"颜色");
//判断颜色是否合法
if(c.isValid())
{
//将改颜色添加到当前选中的版本
//ui->msgEdit->setTextColor(c); //设置字体颜色前景色,也就是字体颜色
ui->msgEdit->setTextBackgroundColor(c); //设置背景色
}
}
//打开文件按钮对应的槽函数
void Widget::on_openFile_clicked()
{
//1,找到打开文件的路径
QString filename = QFileDialog::getOpenFileName(
this, //父组件
"选择", //窗口名
"./", //起始路径
"Txt(*.txt);;C程序(*.c);;C++(*.cpp))"); //过滤器
//qDebug() << filename;
//2,使用QFile实例化一个对象,可以用获取的路径名进行构造
QFile f(filename);
//3,打开文件
if(!f.open(QFile::ReadWrite)) //以读取形式打开文件
{
return;
}
//4,读取文件内容
QByteArray msg = f.readAll();
//5,将读取出来的内容放到ui中
ui->msgEdit->setText(msg);
//关闭文件
f.close();
// ui->msgEdit->toPlainText();
}
//保存文件
void Widget::on_saveFile_clicked()
{
//1,指定保存路径名
QString filename = QFileDialog::getSaveFileName(
this, //父组件
"选择保存路径", //窗口名
"./new", //起始路径
"Txt(*.txt);;C程序(*.c);;C++(*.cpp))"); //过滤器
//2,使用QFile实例化一个对象,可以用获取的路径名进行构造
QFile f(filename);
//3,打开文件
if(!f.open(QFile::WriteOnly))
{
return;
}
//4,读取ui内容
QString s = ui->msgEdit->toPlainText();
//5,将获取到的内容转换成C
QByteArray msg = s.toLocal8Bit();
//6,将读取出来的内容放到文件中
f.write(msg);
//关闭
f.close();
}
QT-day3,消息对话框
于 2023-04-27 21:07:09 首次发布