当前位置:网站首页>QT专题:组合会话框和文本编辑器
QT专题:组合会话框和文本编辑器
2022-08-02 09:29:00 【迷途君】
目录
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()
{
}
边栏推荐
- 【SeaTunnel】从一个数据集成组件演化成企业级的服务
- 带你认识40G单纤双向光模块-QSFP+ BiDi光模块
- 曲折的tensorflow安装过程(Tensorflow 安装问题的解决)
- Spend 2 hours a day to make up for Tencent T8, play 688 pages of SSM framework and Redis, and successfully land on Meituan
- ABAP 和json转换的方法
- 膜拜,Alibaba分布式系统开发与核心原理解析手册
- 从零开始入门单片机(一):必会背景知识总结
- YugaByte adds Voyager migration service in its 2.15 database update
- Daily practice of dynamic programming (3)
- SVN下载上传文件
猜你喜欢

Jetpack Compose 中的状态管理

2022牛客暑期多校训练营4(ADHKLMN)

SVN下载上传文件

边缘计算开源项目概述

在全志V853开发板试编译QT测试

要长续航还是更安全?海豹与深蓝SL03对比导购

The god-level Alibaba "high concurrency" tutorial "basic + actual combat + source code + interview + architecture"

高效时代,电商运营如何靠RPA快速提效?

Re22:读论文 HetSANN An Attention-based Graph Neural Network for Heterogeneous Structural Learning

向量组的线性相关性
随机推荐
AutoJs学习-实现科赫雪花
每天花2小时恶补腾讯T8纯手打688页SSM框架和Redis,成功上岸美团
tf中tensor的大小输出
Jenkins--基础--6.3--Pipeline--语法--脚本式
The 17th day of the special assault version of the sword offer
Re23:读论文 How Does NLP Benefit Legal System: A Summary of Legal Artificial Intelligence
日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
XML简介
初学者怎么快速学会SQL
边缘计算开源项目概述
记某社区问答
关于缓存数据的探讨
Naive Bayesian Method of Li Hang's "Statistical Learning Methods" Notes
Pytorch's LSTM parameters explained
理解JS的三座大山
曲折的tensorflow安装过程(Tensorflow 安装问题的解决)
数据库mysql
cococreator 动态设置精灵
Using the TCP protocol, will there be no packet loss?
大厂外包,值得拥有吗?