#include "window.h"
#include "ui_window.h"
Window::Window(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Window)
{
this->setWindowIcon(QIcon("E:/QT/qt-pp/icon_/denglu.png"));
this->setWindowTitle("小沈文档");
ui->setupUi(this);
}
//让文本和窗口大小一起变换
void Window::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
ui->textEdit->resize(event->size()); // textBrowser是显示文档的组件名
}
Window::~Window()
{
delete ui;
}
//打开一个文件
void Window::on_openbtn_clicked()
{
//获取文件路径
QString fileName = QFileDialog::getOpenFileName(this,
"Open File",
"./",
"ALL(*.*)");
qDebug()<<fileName;
QFile file(fileName);//构造一个新的文件对象来表示具有给定名称的文件。QFile::QFile(const QString &name)
//判断文件是否存在
if(!file.exists())
{
qDebug()<<"文件不存在";
return;
}
//打开一个文件
if(!file.open(QIODevice::ReadWrite))
{
qDebug()<<"打开失败";
}
//读取界面中的内容
QByteArray text = file.readAll();
//将读取的内容拷贝到界面中
ui->textEdit->setText(QString::fromLocal8Bit(text));
file.close();
}
//保存一个文件
void Window::on_savebtn_clicked()
{
//获取文件路径
QString fileName = QFileDialog::getSaveFileName(this,
"save File",
"./",
"ALL(*.*)");
QFile qfile (fileName);//构造一个新的文件对象来表示具有给定名称的文件。QFile::QFile(const QString &name)
//打开一个文件
if(!qfile.open( QIODevice::ReadWrite))
{
qDebug()<<"打开失败";
return;
}
//获取界面中的内容
QString msg=ui->textEdit->toPlainText();
//将界面中的内容保存到文件中
qfile.write(msg.toLocal8Bit());
//关闭文件
qfile.close();
}
void Window::on_footbtn_clicked()
{
bool ok;
QFont font = QFontDialog::getFont(
&ok, QFont("宋体", 20,20), this);
if (ok) {
qDebug()<< "the user clicked OK and font is set to the font the user selected";
} else {
qDebug()<< "the user canceled the dialog; font is set to the initial";
}
}
void Window::on_colorbtn_clicked()
{
QColor c=QColorDialog::getColor (Qt::black,this, "选择颜色");
if(c.isValid())
{
ui->textEdit->setTextColor(c);
}
else {
qDebug()<<"用户取消选择颜色";
}
}
Qt day3
最新推荐文章于 2024-07-23 15:39:41 发布