#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>
#include <QShortcut>
#include <QWheelEvent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->textEdit->installEventFilter(this);
QShortcut *shortcutOpen = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")),this);
QShortcut *shortcutSave = new QShortcut(QKeySequence(tr("Ctrl+S", "File|Save")),this);
QShortcut *shortcutZoomIn = new QShortcut(QKeySequence(tr("Ctrl+Shift+=", "File|Save")),this);
QShortcut *shortcutZoomOut = new QShortcut(QKeySequence(tr("Ctrl+Shift+-", "File|Save")),this);
connect(shortcutOpen,&QShortcut::activated,[=](){
on_openButton_clicked();
});
connect(shortcutSave,&QShortcut::activated,[=](){
on_saveButton_clicked();
});
connect(shortcutZoomIn,&QShortcut::activated,[=](){
zoomIn();
});
connect(shortcutZoomOut,&QShortcut::activated,[=](){
zoomOut();
});
//虽然上面一行代码进行widget和ui的窗口关联,但是如果发生窗口大小变化的时候,里面的布局不会随之变化
//通过下面这行代码进行显示说明,让窗口变化时,布局及其子控件随之调整
this->setLayout(ui->verticalLayout);
connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(onCurrentIndexChanged(int)));
connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(onCursorPositionChanged())); //显示行列
}
Widget::~Widget()
{
delete ui;
}
void Widget::zoomIn()
{
//获得TextEdit的当前字体信息
QFont font = ui->textEdit->font();
//获得当前字体的大小
int fontSize = font.pointSize();
if(fontSize == -1) return;
//改变大小,并设置字体大小
int newFontSize = fontSize+1;
font.setPointSize(newFontSize);
ui->textEdit->setFont(font);
}
void Widget::zoomOut()
{
//获得TextEdit的当前字体信息
QFont font = ui->textEdit->font();
//获得当前字体的大小
int fontSize = font.pointSize();
if(fontSize == -1) return;
//改变大小,并设置字体大小
int newFontSize = fontSize-1;
font.setPointSize(newFontSize);
ui->textEdit->setFont(font);
}
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
/* QKeyEvent *keyEvent = (QKeyEvent*)event;
if(keyEvent->key() == Qt::Key_Control){
qDebug() << "Ctrl";
}
*/
if(event->type() == QEvent::Wheel){
if(QGuiApplication::keyboardModifiers() == Qt::ControlModifier){
QWheelEvent *wheelEvent = dynamic_cast<QWheelEvent*>(event);
if(wheelEvent->angleDelta().y() > 0){
zoomIn();
}else if(wheelEvent->angleDelta().y()<0){
zoomOut();
}
return true;
}
return false;
}
}
void Widget::on_openButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"F:/myqt/project_of_qt_new",
tr("Text (*.txt)"));
ui->textEdit->clear();
file.setFileName(fileName);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
qDebug() << "file open error";
}
this->setWindowTitle(fileName + "- MyNoteBook");
QTextStream in(&file);
QString str = ui->comboBox->currentText();//把QString转化成char *
const char* c_str = str.toStdString().c_str();
in.setCodec(c_str);
while(!in.atEnd()){
QString context = in.readLine();
ui->textEdit->append(context);
}
}
void Widget::on_saveButton_clicked()
{
if(!file.isOpen()){
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
"F:/myqt/project_of_qt_new",
tr("Text (*.txt *.doc)"));
file.setFileName(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
qDebug() << "file open error";
}
this->setWindowTitle(fileName + "- MyNoteBook");
}
else
{
file.resize(0);//对于已经打开的文件要先清空再添加, 不然追加到后面就出错啦
}
QTextStream out(&file);
out.setCodec(ui->comboBox->currentText().toStdString().c_str());
QString context = ui->textEdit->toPlainText();
out << context;
}
void Widget::on_closeButton_clicked()
{
QMessageBox msgBox;
int ret = QMessageBox::warning(this, tr("MyNoteBook Notice:"),
tr("The document has been modified.\n"
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel,
QMessageBox::Save);
switch (ret) {
case QMessageBox::Save:
// Save was clicked
on_saveButton_clicked();
qDebug() << "QMessageBox::Save:";
break;
case QMessageBox::Discard:
// Don't Save was clicked
ui->textEdit->clear();
if(file.isOpen()){
file.close();
this->setWindowTitle("MyNoteBook");
}
qDebug() << "QMessageBox::Discard:";
break;
case QMessageBox::Cancel:
// Cancel was clicked
qDebug() << "QMessageBox::Cancel:";
break;
default:
// should never be reached
break;
}
}
void Widget::onCurrentIndexChanged(int index)
{
// 实现功能,对打开的文件修改编码类型,同时能实现重新生成文本框内容
// 先清屏
ui->textEdit->clear();
if(file.isOpen()){ // 判断文件是否打开
//这个函数与修改编码格式的信号绑定了,只能编码类型发生变化的情况下才会使用
QTextStream in(&file); // 实例化对象 -- 来操作这个文件
//修改对应的编码格式
in.setCodec(ui->comboBox->currentText().toStdString().c_str());
//将修改后的文本打印出来
// 注意前面打开文件的时候,光标已经到了最后面,我们需要给光标复位一下
file.seek(0);//光标复位 --> 回到文件头
while(!in.atEnd()){
QString context = in.readLine();
ui->textEdit->append(context);
}
}
}
void Widget::onCursorPositionChanged()
{// 与光标位置改变的信号进行绑定
// 获取文本框的光标位置
QTextCursor cursor = ui->textEdit->textCursor();
// 输出对应行列
qDebug()<<cursor.blockNumber() +1<<',' <<cursor.columnNumber()+1;
QString lableLine = QString::number(cursor.blockNumber() +1);
QString lableRow = QString::number(cursor.columnNumber() +1);
const QString lableMes ="Line:"+lableLine + "Row:"+lableRow+" ";
//const QString lableMes ="行:"+lableLine + "列:"+lableRow+" "; //需要在设置里面把编码格式改为utf-8
qDebug()<<lableMes;
ui->labelLR->setText(lableMes);
}
没有合适的资源?快使用搜索试试~ 我知道了~
项目全部代码,还包含使用到的图片
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共22个文件
png:11个
user:3个
cpp:3个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
0 下载量 69 浏览量
2024-05-21
21:03:02
上传
评论
收藏 60KB RAR 举报
温馨提示
标题中的“项目全部代码,还包含使用到的图片”表明这是一个综合性的项目资源包,其中不仅包括编程项目的源代码,还可能涉及到了项目中使用的各种图片资源。这可能是某个软件开发、网页设计或者数据分析项目的核心组成部分。对于学习、研究或者复制该项目的人来说,这些代码和图片都是至关重要的。 在描述中,内容与标题一致,再次强调了这个压缩包包含项目的所有代码和图片,这暗示着这是一个完整的项目文件,用户可以从中了解到项目从头到尾的设计思路和实现细节。无论是为了学习新技术、理解项目结构,还是为了复刻或改进项目,这个资源都是非常有价值的。 标签“软件/插件 范文/模板/素材”则提供了更广泛的信息,说明这个项目可能涉及到软件开发或者插件的制作,同时也可能包含了可供参考的范例、模板或者设计素材。这对于开发者来说,可能是快速启动新项目的好起点,也可以用于学习如何构建类似的软件或插件。 在“压缩包子文件的文件名称列表”中,我们看到了"notebook03-shijan"。这可能是项目中的一个工作簿或者笔记,很可能是一个Jupyter Notebook文件,通常用于数据处理、分析或者机器学习任务。"shijan"可能是项目的一部分或者特定任务的名称,具体含义可能需要查看文件内容才能确定。 这个压缩包包含的是一整个项目,包括了代码和图片资源,适用于软件开发、插件创建或者数据分析的学习者和实践者。其中的"notebook03-shijan"可能是使用Jupyter Notebook编写的数据分析或实验记录,对于理解项目流程和技术应用具有指导意义。用户可以通过解压文件,逐个查看代码、图片以及notebook,来深入理解项目的实现过程和设计理念,这对于提升个人技能或者借鉴项目经验都非常有帮助。
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 22 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/5a45307947fa452fbd8836250647aa40_mx_jun.jpg!1)
mx_jun
- 粉丝: 1526
- 资源: 11
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)