C++/qt作业

完成文本编辑器的保存功能

firstwindow.cpp

#include "firstwindow.h"
#include "ui_firstwindow.h"

FirstWindow::FirstWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FirstWindow)
{
    ui->setupUi(this);
}

FirstWindow::~FirstWindow()
{
    delete ui;
}

//字体
void FirstWindow::on_btn1_clicked()
{
    bool ok;
    QFont f = QFontDialog::getFont(&ok,
                         QFont("楷体",20,10,false),
                         this,
                         "选择字体");
    if(ok)
    {
        ui->textEdit->setFont(f);
    }else
    {
        QMessageBox::information(this,"提示","您没有选择字体");

    }
}

//颜色
void FirstWindow::on_btn2_clicked()
{
    QColor c = QColorDialog::getColor(QColor(90,100,170),
                                      this,
                                      "选择颜色");
    if(c.isValid())
    {
        ui->textEdit->setTextColor(c);
        //ui->textEdit->setTextBackgroundColor(c);
    }else {
    QMessageBox::information(this,"提示","您没有选择颜色");
}

}

//打开
void FirstWindow::on_btn3_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    "选择文件",
                                                    "./",
                                                    "all file(*.*)");
    QFile file(fileName);
    if(!file.open(QFile::ReadOnly))
    {
        return;
    }
    QByteArray msg = file.readAll();
    ui->textEdit->setText(msg);
    file.close();
}

//保存
void FirstWindow::on_btn4_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                                                    "保存文件",
                                                    "./",
                                                    "all file(*.*)");
    QFile file(fileName);
    if(!file.open(QFile::ReadWrite))
    {
        return;
    }
    file.write(ui->textEdit->toPlainText().toUtf8());

    file.close();
    QMessageBox::information(this,"提示","保存成功");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值