C++&QT-font prompt box, color prompt box, file prompt box, etc.

Table of contents

1.widget.h

2.widget.cpp


1.widget.h

#ifndef WIDGET_H
#define WIDGET_H

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


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_font_btn_clicked();
    // 颜色按钮槽函数
    void on_color_btn_clicked();
    // 打开文件按钮槽函数
    void on_open_btn_clicked();
    // 保存文件按钮槽函数
    void on_save_btn_clicked();

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

2.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_font_btn_clicked()
{
    bool ok; // 返回是否选中字体
    // 调取字体对话框
    QFont f = QFontDialog::getFont (&ok, QFont("简宋", 14, 5, false), this, "字体");
    // 判断是否选择了字体
    if (ok) {
        //ui->textEdit->setFont (f); // 将所有字体进行设置
        ui->textEdit->setCurrentFont (f); // 给选定的当前字体进行设置
    }
}

// 颜色按钮对应槽函数
void Widget::on_color_btn_clicked()
{
    // 调取颜色对话框
    QColor c = QColorDialog::getColor (QColor(255, 255, 255), this, "颜色");
    // 判断颜色是否合法
    if (c.isValid ()) {
        // 将该颜色添加到当前选中的文本
        ui->textEdit->setTextColor (c); // 设置字体颜色
//        ui->textEdit->setTextBackgroundColor (c); // 设置背景颜色
    }
}


// 打开文件按钮对应槽函数
void Widget::on_open_btn_clicked()
{
    // 打开文件对话框,提供文件路径
    QString fileName = QFileDialog::getOpenFileName (this,  // 父组件
                                                    "打开文件", // 标题
                                                    "./",   // 起始路径
                                                    "Txt(*.txt);;C(*.c);;C++(*.cpp);;all(*.)"   // 文件过滤器
                                                    );

    // 使用QFile实例化一个对象
    QFile file(fileName);
    // 打开文件
    if(!file.open (QFile::ReadWrite)){ // 以读写方式打开文件
        return;
    }
    // 读取文件内容,将文件内容放到ui界面
    QByteArray msg = file.readAll (); // 将文件中的内容全部读取出来
    // 将读取出来的内容放到ui界面
    ui->textEdit->setText (msg);

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


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

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

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

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

}

Guess you like

Origin blog.csdn.net/liu319293960_/article/details/130413997