C++day7

widget.cpp

#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_pushButton_clicked()
{
    bool ok;        //返回是否选择字体
    //调出系统的字体对话框
    QFont f = QFontDialog::getFont(&ok,QFont("隶书",10,5,true),this,"字体");
    //功能:调出系统的字体对话框
    //参数一:返回字符选中字体状态
    //参数二:初始字体
    //参数三:父组件
    //参数四:对话框标题

    //将选中的字体设置到文本编辑器中
    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_openfilebBnt_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(
                this,       //父组件
                "选择",       //窗口名
                "./",       //起始路径
                "Txt(*.txt);c程序;;C++程序(*.cpp);;all(*.*)");      //过滤器

    //QDebug()<<fileName;
    //2使用QFile类实例化一个对象,可以用获取的路径名进行构造
    QFile f(fileName);
    //3打开文件
    if(!f.open(QFile::ReadWrite))
    {
        return ;
    }
    //读取文件内容,将文件内容放到ui界面
    QByteArray msg = f.readAll();        //将文件中的内容全部读取出来
    //msg字节数组

    //5.将读取出来的内容放到ui界面上
    ui->msgEdit->setText(msg);

    //6.关闭文件
    f.close();
}

    // 保存文件按钮对应槽函数

void Widget::on_savefileBtn_clicked()
{
    // 打开文件对话框,提供文件路径
    QString fileName = QFileDialog::getSaveFileName (this,  // 父组件
                                                    "保存", // 标题
                                                    "./",   // 起始路径和默认名字
                                                    "Txt(*.txt);;C(*.c);;C++(*.cpp);;all(*.)"   // 文件过滤器
                                                    );

    // 使用QFile实例化一个对象
    QFile file(fileName);
    // 打开文件
    if(!file.open (QFile::WriteOnly)){ // 以写方式打开文件
        return;
    }
    // 获取text中的内容
    QString str = ui->msgEdit->toPlainText ();

    // 写入文件
    file.write (str.toLocal8Bit ()); // 将QString类型转为QByteArray类型

    // 关闭文件
    file.close ();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QFontDialog>      //字体类
#include<QFont>             //字体对话框类
#include<QColorDialog>      //颜色对话框类
#include<QColor>        //颜色类
#include<QFileDialog>
#include<QFile>     //文件类
#include<QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

    void on_colorBtn_clicked();

    void on_openfilebBnt_clicked();

    void on_savefileBtn_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值