QT专题:组合会话框和文本编辑器

目录

文本编辑器案例


QT中已经帮我们内置了一些对话框,可以帮助我们快速的完成一些基础功能界面的绘制,接下来我会介绍一些基础的会话框,并且通过这些会话框完成文本编辑器基础功能的实现。

1.颜色对话框 QColor

bt_color = new QPushButton("颜色");//创建一个颜色按钮
connect(bt_color, &QPushButton::clicked, this, &Widget::get_color);//将按钮和后台槽函数进行链接
QColor color = QColorDialog::getColor();  //弹出一个颜色对话框,并且返回用户选择的颜色

 2.错误消息提示框 QErrorMessage

我们经常可以看到一些软件会有错误警告框,在软件发生错误时,提示用户,QT也给我们提供了这样一个消息框。

 bt_errmsg = new QPushButton("错误消息");
 connect(bt_errmsg, SIGNAL(clicked(bool)), this, SLOT(show_errmsg()));
 void Widget::show_errmsg()
{
    QErrorMessage emg; //TODO
    emg.showMessage("aaaaaaaaaaaaaaaaaaaaaa");
    emg.exec();//模态显示,只能操作当前会话框
}

 3.文件路径选择框 GFileDialog

bt_file = new QPushButton("文件路径选择");
connect(bt_file, SIGNAL(clicked(bool)), this, SLOT(get_filepath()));
void Widget::get_filepath()
{
    //QString filepath = QFileDialog::getOpenFileName(); //弹出一个文件选择对话框,并且返回文件路径
    QStringList filepaths = QFileDialog::getOpenFileNames(this, "打开文件", "C:\\Users\\admin\\Desktop\\12-CPP-QT课程已加密", "Images (*.png *.bmp *.jpg)"); //弹出一个文件选择对话框,并且返回一堆文件路径
    for(int i=0; i<filepaths.length(); i++)
        te->append(filepaths[i]);
}

4.字体选择框 QFont

在一些文本编辑器中都有将文字字体改变的功能,而QT也给我们提供了这样一个会话框。

 bt_font = new QPushButton("字体选择");
 connect(bt_font, &QPushButton::clicked, this, &Widget::get_font);
void Widget::get_font()
{
//    QFontDialog *d = new QFontDialog;
//    d->exec();
    bool ok;
    QFont font = QFontDialog::getFont(&ok);  //弹出字体对话框,并且界面小时的时候反馈用户选择的字体

    if(ok)
    {
        //te->setFont(font);        //设置整个文本框的字体
        te->setCurrentFont(font);   //设置被选中的文字的字体
    }
}

注意:QFontDialog::getFont()表示该函数是这个字体类的一个静态成员函数,所以可以直接这样调用。

5. 输入框 QInputDialog

bt_input = new QPushButton("输入框");
 connect(bt_input, SIGNAL(clicked(bool)), this, SLOT(get_input()));
void Widget::get_input()
{
    QString name = QInputDialog::getText(this, "xxx", "你的名字");//该函数也是输入类的一个静态函数
    te->append(name);
}

 6.消息框 QMessageBox

bt_msg = new QPushButton("消息");
connect(bt_msg, SIGNAL(clicked(bool)), this, SLOT(show_msg()));
void Widget::show_msg()
{
    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);//设置选项的函数
    qDebug() << msgBox.exec();
}

setStandarButtons是一个消息类设置选项的一个成员函数这里通过对象进行调用。

7.进度框 QProgressDialog

bt_progress = new QPushButton("进度");
connect(bt_progress, SIGNAL(clicked(bool)), this, SLOT(show_progress()));
void Widget::show_progress()
{
    QProgressDialog progress("正在下载", "取消", 0, 100);
    progress.setValue(60);
    progress.exec();
}

 下面我们可以通过上面这些知识编写一个文本编辑器实现一些基本的功能了

文本编辑器案例

这里补充一个小知识,如果你想要将一些图标图片资源和程序放在一起进行编译,那么可以创建resources文件,将图片放进去,这样在程序中使用起来会更方便。

 

 图片中可以看到我们使用了mainwindow这个窗口,当我们功能比较多时就会选择用这种类型的窗口界面来进行编程。

1.首先我们要将文本编辑器所有的功能动作action创建出来,并将他们和槽函数进行关联,来实现对应的功能。

    //0. 构造所有的action
    QAction *new_action = new QAction(QIcon(":/img/new.png"), "新建");
    new_action->setShortcut(QKeySequence("Ctrl+N"));
    connect(new_action, SIGNAL(triggered(bool)), this, SLOT(new_file()));

    QAction *open_action = new QAction(QIcon(":/img/open.png"), "打开");
    open_action->setShortcut(QKeySequence("Ctrl+O"));
    connect(open_action, SIGNAL(triggered(bool)), this, SLOT(open_file()));
    //open_action->setEnabled(false);

    QAction *saveas_action = new QAction(QIcon(":/img/saveas.png"), "另存为");
    saveas_action->setShortcut(QKeySequence("Ctrl+Y"));
    connect(saveas_action, SIGNAL(triggered(bool)), this, SLOT(saveas_file()));

    QAction *save_action = new QAction(QIcon(":/img/save.png"), "保存");
    save_action->setShortcut(QKeySequence("Ctrl+s"));
    connect(save_action, SIGNAL(triggered(bool)), this, SLOT(save_file()));

    QAction *close_action = new QAction(QIcon(":/img/close.png"), "关闭");
    close_action->setShortcut(QKeySequence("Ctrl+X"));
    connect(close_action, SIGNAL(triggered(bool)), this, SLOT(close_file()));

    QAction *font_action = new QAction(QIcon(":/img/font.png"), "字体");
    font_action->setShortcut(QKeySequence("Ctrl+f"));
    connect(font_action, SIGNAL(triggered(bool)), this, SLOT(set_font()));

    QAction *color_action = new QAction(QIcon(":/img/color.png"), "颜色");
    color_action->setShortcut(QKeySequence("Ctrl+f"));
    connect(color_action, SIGNAL(triggered(bool)), this, SLOT(set_color()));

    QAction *copy_action = new QAction(QIcon(":/img/copy.png"), "复制");
    copy_action->setShortcut(QKeySequence("Ctrl+c"));
    connect(copy_action, SIGNAL(triggered(bool)), this, SLOT(set_copy()));
    
    QAction *paste_action = new QAction(QIcon(":/img/paste.png"), "粘贴");
    paste_action->setShortcut(QKeySequence("Ctrl+v"));
    connect(paste_action, SIGNAL(triggered(bool)), this, SLOT(set_paste()));
    
    QAction *cut_action = new QAction(QIcon(":/img/cut.png"), "剪切");
    cut_action->setShortcut(QKeySequence("Ctrl+r"));
    connect(cut_action, SIGNAL(triggered(bool)), this, SLOT(set_cut()));

2.获取菜单栏,并给菜单栏添加选项

    //1. 获取菜单栏menuBar(),添加菜单->addMenu,添加选项
    QMenu *fileMenu = menuBar()->addMenu("&File"); //&F: 用键盘Alt+f
    fileMenu->addAction(new_action);
    fileMenu->addAction(open_action);
    fileMenu->addAction(save_action);
    fileMenu->addAction(saveas_action);
    fileMenu->addAction(close_action);
    fileMenu->addAction(copy_action);
    fileMenu->addAction(paste_action);
    fileMenu->addAction(cut_action);

    QMenu *editMenu = menuBar()->addMenu("&Edit"); //&E: 用键盘Alt+e
    editMenu->addAction(font_action);
    editMenu->addAction(color_action);

3.将一些常用的功能放到工具栏

    //2. 工具栏
    QToolBar *filetoolbar = addToolBar("file");   //添加一个工具栏,并且放入常用的action
    filetoolbar->addAction(new_action);
    filetoolbar->addAction(open_action);
    filetoolbar->addAction(save_action);
    filetoolbar->addAction(saveas_action);
    filetoolbar->addAction(close_action);
    filetoolbar->addAction(copy_action);
    filetoolbar->addAction(paste_action);
    filetoolbar->addAction(cut_action);

 QToolBar *edittoolbar = addToolBar("edit");   //添加一个工具栏,并且放入常用的action
    edittoolbar->addAction(font_action);
    edittoolbar->addAction(color_action);

    QToolBar *xxxtoolbar = addToolBar("编译");   //添加一个工具栏,并且放入常用的action
    QToolButton *tb = new QToolButton;
    tb->setText("编译");
    xxxtoolbar->addWidget(tb);

 4.设置中央部件

这里我们只需在中间添加一个文本编辑框即可

    //3. 设置中央部件
    te = new QTextEdit;
    te->setMinimumSize(640, 480);
    this->setCentralWidget(te);
    te->setDisabled(true);

5.设置状态栏

我们可以通过状态栏来展示文件的状态,是否被编辑等

    //4. 状态栏
    lb = new QLabel;
    QStatusBar *st = statusBar();
    st->addWidget(lb);


    connect(te, &QTextEdit::textChanged, [&]{
        if(!lb->text().contains('*'))
            lb->setText(lb->text()+'*');
    });//朗达表达式也叫无名函数,不需要再编写槽函数进行关联使用

这里我们使用了无名函数(朗达表达式的方式来替换原来的槽函数,这是一个比较高级的写法)这里的意思如果文本被编辑内容被改变那么状态栏就会加上*号来展示。

6.编写槽函数完成相应功能

//设置字体的槽函数
void MainWindow::set_font()
{
    qDebug() << "set font........";
    bool ok;
    QFont font = QFontDialog::getFont(&ok);
    if(ok)
        te->setCurrentFont(font);//设置选中的文本内容的字体
}

//设置字体颜色的槽函数
void MainWindow::set_color()
{
    qDebug() << "set color........";
    QColor color = QColorDialog::getColor();
    te->setTextColor(color);
}
//新建文件的槽函数
void MainWindow::new_file()
{
    qDebug() << "new........";
    te->setEnabled(true);
    if(lb->text().contains('*')) //有文件在编辑中
    {
        close_file();
    }
    else
    {
        //清除工作区
        te->clear();
        lb->clear();
    }

}
//打开文件的槽函数
void MainWindow::open_file()
{
    qDebug() << "open........";
    te->setEnabled(true);
    //1. 提取文件路径
    QString path = QFileDialog::getOpenFileName();

    //2. 提取出文件内容 QFile
    QFile f(path);
    f.open(QIODevice::ReadOnly);
    QByteArray buf = f.readAll();
    f.close();

    //3. 将内容显示在文本编辑框
    te->setText(buf);

    //4. 显示文件路径在状态栏
    lb->setText(path);
}
//保存文件内容的槽函数
void MainWindow::save_file()
{
    //1. 提取文件路径
    QString str = lb->text();
    if(!str.contains('*')) //如果文件未有改动
        return;
    str.chop(1);
    if(str.isEmpty()) //这是一个新建的文件
    {
        saveas_file();
        return;
    }

    //2. 提取文本编辑框的内容
    QString buf = te->toPlainText();

    //3. 将内容写入文件
    QFile f(str);
    f.open(QIODevice::WriteOnly);
    f.write(buf.toStdString().c_str());
    f.close();

    //4. 更新状态栏
    lb->setText(str);
}
//将文件另存为的槽函数
void MainWindow::saveas_file()
{
    //1. 提取文件路径
    QString str = QFileDialog::getSaveFileName();
    if(str.isEmpty())
        return;

    //2. 提取文本编辑框的内容
    QString buf = te->toPlainText();

    //3. 将内容写入文件
    QFile f(str);
    f.open(QIODevice::WriteOnly);
    f.write(buf.toStdString().c_str());
    f.close();

    //3. 更新状态栏
    lb->setText(str);
}
//关闭文件的槽函数
void MainWindow::close_file()
{
    qDebug() << "close........";
    if(lb->text().contains('*')) //有文件被编辑中
    {
        //1. 弹框提示是否需要保存
        QMessageBox msgBox;
        msgBox.setText("The document has been modified.");
        msgBox.setInformativeText("Do you want to save your changes?");
        msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Save);
        int ret = msgBox.exec();

        //2. 如果要保存
        if(QMessageBox::Save == ret)
            save_file();
        else if(QMessageBox::Cancel == ret)
            return;
    }

    //清除工作区
    te->clear();
    lb->clear();
}
void MainWindow::set_copy()
{
    te->copy();
}
void MainWindow::set_paste()
{
    te->paste();
}
void MainWindow::set_cut()
{
    te->cut();
}
MainWindow::~MainWindow()
{

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值