#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);
}
没有合适的资源?快使用搜索试试~ 我知道了~
项目全部代码,还包含使用到的图片
共11个文件
user:3个
cpp:3个
h:2个
0 下载量 71 浏览量
2024-05-21
21:01:57
上传
评论
收藏 14KB RAR 举报
温馨提示
项目全部代码,还包含使用到的图片
资源推荐
资源详情
资源评论
收起资源包目录
notebook03-shijan.rar (11个子文件)
notebook03-shijan
widget.ui 7KB
notebook03-使用事件过滤器.user 24KB
mytextedit.h 376B
widget.cpp 7KB
notebook03-shijan.pro 1KB
mytextedit.cpp 1KB
main.cpp 175B
res.qrc 2KB
notebook03-shijan.user 24KB
notebook03-shijan.pro.user 24KB
widget.h 713B
共 11 条
- 1
资源评论
mx_jun
- 粉丝: 1155
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功