赞
踩
开发流程
打开文件
把文件数据显示在TextEdit控件
代码实现
void Widget::on_btnOpen_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "F:/Qt/", tr("Text (*.txt)")); ui->textEdit->clear();//刷新文本控件 file.setFileName(fileName); if(!file.open(QIODevice::ReadOnly|QIODevice::Text)) { qDebug()<<"file open error"; } QTextStream in(&file); in.setCodec("UTF-8"); while (!in.atEnd()) { QString str = in.readLine(); ui->textEdit->append(str); //追加到控件 } }
开发流程
判断当下是否有已经打开的文件,如果有打开的文件
读取TextEdit的内容
写入新文件
代码实现
void Widget::on_btnSave_clicked() { QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "F:/Qt/untitled.txt", tr("Text (*.txt *.doc)")); file.setFileName(fileName); if(!file.open(QIODevice::WriteOnly|QIODevice::Text)) { qDebug()<<"file open error"; } QTextStream out(&file); out.setCodec("UTF-8"); QString context=ui->textEdit->toPlainText(); out << context; }
开发流程
文件是否打开,打开就关闭;没打开就不用处理
代码实现
void Widget::on_btnClose_clicked()
{
if(file.isOpen())
{
file.close();
ui->textEdit->clear();
}
}
获取用户在QComboBox上选择的字符编码,用特定编码打开文件,这里注意QComboBox返回QString类型,
setCodec参数要求const char*型
QString先转成C++的String,再转换成const char *
void Widget::on_btnFileOpen_clicked() { // 使用文件对话框获取要打开的文件的路径 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "D:/QT/", tr("Text (*.txt)")); // 清空文本编辑器的内容 ui->textEdit->clear(); // 设置 QFile 对象的文件名 file.setFileName(fileName); // 尝试以只读和文本模式打开文件 if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){ // 如果文件打开失败,输出错误信息 qDebug() << "file open error"; } // 创建 QTextStream 用于读取文件内容 QTextStream in(&file); // 从下拉框获取当前选中的字符编码 QString str = ui->comboBox->currentText(); // 将 QString 转化为 char* 类型 const char* c_str = str.toStdString().c_str(); // 设置 QTextStream 的字符编码 in.setCodec(c_str); // 循环读取文件直到结束 while(!in.atEnd()){ // 读取文件的一行 QString context = in.readLine(); // 将读取的内容追加到文本编辑器 ui->textEdit->append(context); } }
//1. 在Widget的构造函数中关联信号与槽,检测用户选择条目的信号。
connect(ui-
>comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(onCurrentIndexChanged(int)))
;
//2. 添加槽函数,当用户选择信号后被调用,判断是否当前有打开的文件,如果有,则重新用新的编码读取文 件并重新显示。 // onCurrentIndexChanged 方法:当 QComboBox 的选中项变化时执行 void Widget::onCurrentIndexChanged(int index) { // 输出调试信息,表示此槽函数被触发 qDebug() << "currentIndexChanged Signal"; // 清空文本编辑器的内容 ui->textEdit->clear(); // 检查文件是否已经打开 if(file.isOpen()){ // 输出调试信息,表示文件是打开状态 qDebug() << "file is Open"; // 创建 QTextStream 用于读取文件内容 QTextStream in(&file); // 设置 QTextStream 的字符编码为 QComboBox 当前选中的编码 in.setCodec(ui->comboBox->currentText().toStdString().c_str()); // 将文件指针移动到文件开始位置 file.seek(0); // 循环读取文件直到文件结束 while(!in.atEnd()){ // 读取文件的一行 QString context = in.readLine(); // 将读取的内容追加到文本编辑器 ui->textEdit->append(context); } } }
使用QTextEdit的cursorPositionChanged信号,当光标发生移动时候刷新显示
//1. 在构造函数中添加信号与槽
connect(ui-
>textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(onCursorPositionChanged()));
//2. 槽函数获取textEdit的行列并显示到QLabel上
void Widget::onCursorPositionChanged()
{
QTextCursor cursor = ui->textEdit->textCursor();
//qDebug() << cursor.blockNumber()+1 <<","<< cursor.columnNumber() + 1;
QString blockNum = QString::number(cursor.blockNumber()+1);
QString columnNum = QString::number(cursor.columnNumber()+1);
const QString labelMes = "L:"+blockNum+",C:"+columnNum+" ";
//const QString labelMes = "行:"+blockNum+",列:"+columnNum+" ";
ui->labelPosition->setText(labelMes);
}
文件打开提示
this->setWindowTitle(fileName+"-MynoteBook"); //记事本打开提示
文件保存也同理
文件关闭
this->setWindowTitle("MynoteBook"); //记事本关闭提示
QComboBox
QComboBox 是 Qt 框架中用于创建下拉列表的一个控件。
它允许用户从一组选项中选择一个选项,并可以配置为可编辑,使用户能够在其中输入文本。
QComboBox 提供了一系列方法来添加、删除和修改列表中的项,支持通过索引或文本检索项,并可以通过信号和槽机制来响应用户的选择变化。该控件广泛应用于需要从多个选项中进行选择的用户界面场景,例如表单和设置界面
打开功能
//记事本打开功能 void Widget::on_btnOpen_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "F:/Qt/", 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); // in.setCodec("UTF-8"); QString str=ui->comboBox->currentText();//把Qstring转换为char* const char* c_str=str.toStdString().c_str(); in.setCodec(c_str); while (!in.atEnd()) { QString str = in.readLine(); ui->textEdit->append(str); //追加到控件 } }
记事本支持字符编码
connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(oncurrentIndexChanged(int))); //捕捉字符编码信号
//字符编码
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 str = in.readLine();
ui->textEdit->append(str); //追加到控件
}
}
}
使用QTextEdit的cursorPositionChanged信号,当光标发生移动时候刷新显示
//1. 在构造函数中添加信号与槽
connect(ui-
>textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(onCursorPositionChanged()));
//2. 槽函数获取textEdit的行列并显示到QLabel上
void Widget::onCursorPositionChanged()
{
QTextCursor cursor = ui->textEdit->textCursor();
//qDebug() << cursor.blockNumber()+1 <<","<< cursor.columnNumber() + 1;
QString blockNum = QString::number(cursor.blockNumber()+1);
QString columnNum = QString::number(cursor.columnNumber()+1);
const QString labelMes = "L:"+blockNum+",C:"+columnNum+" ";
//const QString labelMes = "行:"+blockNum+",列:"+columnNum+" ";
ui->labelPosition->setText(labelMes);
}
显示光标行列值
//光标行列值 void Widget::oncursorPositionChanged() { QTextCursor cursor=ui->textEdit->textCursor(); //qDebug()<<cursor.blockNumber()+1<<","<<cursor.columnNumber()+1; QString blockNum=QString::number(cursor.blockNumber()+1); QString columNum=QString::number(cursor.columnNumber()+1); const QString labelMes="L:"+blockNum+",C"+columNum+" "; ui->labelposition->setText(labelMes); //设置当前行高亮 QList<QTextEdit::ExtraSelection>extraSelection; QTextEdit::ExtraSelection ext; //1.知道当前行 ext.cursor=cursor; QBrush qBrush(Qt::lightGray); //2.颜色 ext.format.setBackground(qBrush); //配置段属性:整行显示,没有不行 ext.format.setProperty(QTextFormat::FullWidthSelection,true); //3.设置 //把ext加到ext的容器中 extraSelection.append(ext); ui->textEdit->setExtraSelections(extraSelection); }
this->setWindowTitle(fileName + "- MyNoteBook");
开发流程
void Widget::on_btnSave_clicked() { //如果当前没有文件打开,就弹窗让用户选择新文件,创建新文件,而不是原来那样,都弹出新的 文件保存窗口 if(!file.isOpen()){ QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "D:/QT/untitled.txt", tr("Text (*.txt *.doc)")); file.setFileName(fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){ qDebug() << "file open error"; } this->setWindowTitle(fileName + "- MyNoteBook"); } //当保存被按下,不管是已有打开的文件还是上面if满足后用户选择新文件,都要读取TextEdit 内容并写入文件中 QTextStream out(&file); out.setCodec(ui->comboBox->currentText().toStdString().c_str()); QString context = ui->textEdit->toPlainText(); out << context; }
//记事本关闭功能 void Widget::on_btnClose_clicked() { QMessageBox msgBox; //弹窗组件 int ret = QMessageBox::warning(this, tr("My NoteBook 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: on_btnSave_clicked(); qDebug()<<"QMessageBox::Save"; break; case QMessageBox::Discard: ui->textEdit->clear(); if(file.isOpen()) { file.close(); this->setWindowTitle("MynoteBook"); //记事本关闭提示 } qDebug()<< "QMessageBox::Discard"; break; case QMessageBox::Cancel: qDebug()<<"QMessageBox::Cancel"; break; default: qDebug()<<"should never be reached"; break; } }
QShortcut *shortcutOpen = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")),this);
QShortcut *shortcutSave = new QShortcut(QKeySequence(tr("Ctrl+S", "File|Save")),this);
QShortcut *shortzoomIn = new QShortcut(QKeySequence(tr("Ctrl+Shift+=", "File|Save")),this);
QShortcut *shortzoomOut = new QShortcut(QKeySequence(tr("Ctrl+Shift+-", "File|Save")),this);
//打开快捷键 connect(shortcutOpen,&QShortcut::activated,[=](){ on_btnOpen_clicked(); }); //保存快捷键 connect(shortcutSave,&QShortcut::activated,[=](){ on_btnSave_clicked(); }); connect(shortzoomIn,&QShortcut::activated,[=](){ zoomIn(); }); connect(shortzoomOut,&QShortcut::activated,[=](){ zoomOut(); });
//快捷键放大
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);
}
mytextedit.h
#ifndef MYTEXTEDIT_H #define MYTEXTEDIT_H #include <qtextedit.h> class MyTextEdit : public QTextEdit { public: MyTextEdit(QWidget *parent); void wheelEvent(QWheelEvent *e) override; void keyPressEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override; private: bool ctrlKetPressed=0; }; #endif // MYTEXTEDIT_H
mytextedit.cpp
#include "mytextedit.h" #include <QWheelEvent> #include <QDebug> MyTextEdit::MyTextEdit(QWidget *parent):QTextEdit(parent) { } void MyTextEdit::wheelEvent(QWheelEvent *e) { if(ctrlKetPressed==1) { qDebug()<<e->angleDelta().y(); if(e->angleDelta().y()>0) { zoomIn(); } else { zoomOut(); } e->accept();// } else { QTextEdit::wheelEvent(e); } } void MyTextEdit::keyPressEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Control) { qDebug()<<"ctrl pressed"; ctrlKetPressed=1; } QTextEdit::keyPressEvent(e); } void MyTextEdit::keyReleaseEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Control) { qDebug()<<"ctrl released"; ctrlKetPressed=0; } QTextEdit::keyReleaseEvent(e); }
事件过滤器实现字体大小
bool Widget::eventFilter(QObject *watched, QEvent *event) { /* if(event->type()==QEvent::MouseButtonPress) { qDebug()<<"MouseButtonPress"; } */ /* QKeyEvent *keyevent=(QKeyEvent*)event; if(keyevent->key()==Qt::Key_Control) { } */ if(event->type()==QEvent::Wheel) { if(QGuiApplication::keyboardModifiers()==Qt::ControlModifier) { qDebug()<<"ctrl+wheel"; QWheelEvent *wheelevent=dynamic_cast<QWheelEvent*>(event); if(wheelevent->angleDelta().y()>0) { zoomIn(); } else if(wheelevent->angleDelta().y()<0) { zoomOut(); } return true; //ctrl键被按下 } return false; //ctrl键没被按下 } }
代码一
mytextedit.h
#ifndef MYTEXTEDIT_H #define MYTEXTEDIT_H #include <qtextedit.h> class MyTextEdit : public QTextEdit { public: MyTextEdit(QWidget *parent); void wheelEvent(QWheelEvent *e) override; void keyPressEvent(QKeyEvent *e) override; void keyReleaseEvent(QKeyEvent *e) override; private: bool ctrlKetPressed=0; }; #endif // MYTEXTEDIT_H
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QFile> #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: QFile file; void zoomIn(); void zoomOut(); Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_btnOpen_clicked(); void on_btnSave_clicked(); void on_btnClose_clicked(); void oncurrentIndexChanged(int index); void oncursorPositionChanged(); private: Ui::Widget *ui; }; #endif // WIDGET_H
mytextedit.cpp
#include "mytextedit.h" #include <QWheelEvent> #include <QDebug> MyTextEdit::MyTextEdit(QWidget *parent):QTextEdit(parent) { } void MyTextEdit::wheelEvent(QWheelEvent *e) { if(ctrlKetPressed==1) { qDebug()<<e->angleDelta().y(); if(e->angleDelta().y()>0) { zoomIn(); } else { zoomOut(); } e->accept();// } else { QTextEdit::wheelEvent(e); } } void MyTextEdit::keyPressEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Control) { qDebug()<<"ctrl pressed"; ctrlKetPressed=1; } QTextEdit::keyPressEvent(e); } void MyTextEdit::keyReleaseEvent(QKeyEvent *e) { if(e->key()==Qt::Key_Control) { qDebug()<<"ctrl released"; ctrlKetPressed=0; } QTextEdit::keyReleaseEvent(e); }
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QFileDialog> #include<QDebug> #include <QMessageBox> #include <QShortcut> //快捷键放大 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); } Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); QShortcut *shortcutOpen = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")),this); QShortcut *shortcutSave = new QShortcut(QKeySequence(tr("Ctrl+S", "File|Save")),this); QShortcut *shortzoomIn = new QShortcut(QKeySequence(tr("Ctrl+Shift+=", "File|Save")),this); QShortcut *shortzoomOut = new QShortcut(QKeySequence(tr("Ctrl+Shift+-", "File|Save")),this); //打开快捷键 connect(shortcutOpen,&QShortcut::activated,[=](){ on_btnOpen_clicked(); }); //保存快捷键 connect(shortcutSave,&QShortcut::activated,[=](){ on_btnSave_clicked(); }); connect(shortzoomIn,&QShortcut::activated,[=](){ zoomIn(); }); connect(shortzoomOut,&QShortcut::activated,[=](){ zoomOut(); }); this->setLayout(ui->verticalLayout); ui->widgetBottom->setLayout(ui->horizontalLayout); connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(oncurrentIndexChanged(int))); //捕捉字符编码信号 connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(oncursorPositionChanged())); //捕捉光标行列值 } Widget::~Widget() { delete ui; } //记事本打开功能 void Widget::on_btnOpen_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "F:/Qt/", 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); // in.setCodec("UTF-8"); QString str=ui->comboBox->currentText();//把Qstring转换为char* const char* c_str=str.toStdString().c_str(); in.setCodec(c_str); while (!in.atEnd()) { QString str = in.readLine(); ui->textEdit->append(str); //追加到控件 } } //记事本保存功能 void Widget::on_btnSave_clicked() { if(!file.isOpen()) //文件是否打开 { QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "F:/Qt/untitled.txt", tr("Text (*.txt *.doc)")); file.setFileName(fileName); if(!file.open(QIODevice::WriteOnly|QIODevice::Text)) { qDebug()<<"file open error"; } this->setWindowTitle(fileName+"-MynoteBook"); //记事本保存提示 } QTextStream out(&file); out.setCodec(ui->comboBox->currentText().toStdString().c_str()); QString context=ui->textEdit->toPlainText(); out << context; } //记事本关闭功能 void Widget::on_btnClose_clicked() { QMessageBox msgBox; //弹窗组件 int ret = QMessageBox::warning(this, tr("My NoteBook 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: on_btnSave_clicked(); qDebug()<<"QMessageBox::Save"; break; case QMessageBox::Discard: ui->textEdit->clear(); if(file.isOpen()) { file.close(); this->setWindowTitle("MynoteBook"); //记事本关闭提示 } qDebug()<< "QMessageBox::Discard"; break; case QMessageBox::Cancel: qDebug()<<"QMessageBox::Cancel"; break; default: qDebug()<<"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 str = in.readLine(); ui->textEdit->append(str); //追加到控件 } } } //光标行列值 void Widget::oncursorPositionChanged() { QTextCursor cursor=ui->textEdit->textCursor(); //qDebug()<<cursor.blockNumber()+1<<","<<cursor.columnNumber()+1; QString blockNum=QString::number(cursor.blockNumber()+1); QString columNum=QString::number(cursor.columnNumber()+1); const QString labelMes="L:"+blockNum+",C"+columNum+" "; ui->labelposition->setText(labelMes); //设置当前行高亮 QList<QTextEdit::ExtraSelection>extraSelection; QTextEdit::ExtraSelection ext; //1.知道当前行 ext.cursor=cursor; QBrush qBrush(Qt::lightGray); //2.颜色 ext.format.setBackground(qBrush); //配置段属性:整行显示,没有不行 ext.format.setProperty(QTextFormat::FullWidthSelection,true); //3.设置 //把ext加到ext的容器中 extraSelection.append(ext); ui->textEdit->setExtraSelections(extraSelection); }
代码二
widget.h
#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_btnFileOpen_clicked(); }); connect(shortcutSave,&QShortcut::activated,[=](){ on_btnSave_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) { /* if(event->type()==QEvent::MouseButtonPress) { qDebug()<<"MouseButtonPress"; } */ /* QKeyEvent *keyevent=(QKeyEvent*)event; if(keyevent->key()==Qt::Key_Control) { } */ if(event->type()==QEvent::Wheel) { if(QGuiApplication::keyboardModifiers()==Qt::ControlModifier) { qDebug()<<"ctrl+wheel"; QWheelEvent *wheelevent=dynamic_cast<QWheelEvent*>(event); if(wheelevent->angleDelta().y()>0) { zoomIn(); } else if(wheelevent->angleDelta().y()<0) { zoomOut(); } return true; //ctrl键被按下 } return false; //ctrl键没被按下 } } void Widget::on_btnFileOpen_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "F:/Qt/", 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); // in.setCodec("UTF-8"); // in.setCodec("ANSI"); //in.setCodec(ui->comboBox->currentText().toStdString().c_str()); 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_btnSave_clicked() { if(!file.isOpen()){ QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "F:/Qt/untitled.txt", tr("Text (*.txt *.doc)")); file.setFileName(fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){ qDebug() << "file open error"; } this->setWindowTitle(fileName + "- MyNoteBook"); } QTextStream out(&file); out.setCodec(ui->comboBox->currentText().toStdString().c_str()); QString context = ui->textEdit->toPlainText(); out << context; } void Widget::on_btnClose_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_btnSave_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) { qDebug() << "currentItSignal"; ui->textEdit->clear(); if(file.isOpen()){ qDebug() << "file is Open"; 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 blockNum = QString::number(cursor.blockNumber()+1); QString columnNum = QString::number(cursor.columnNumber()+1); const QString labelMes = "L:"+blockNum+",C:"+columnNum+" "; //const QString labelMes = "行:"+blockNum+",列:"+columnNum+" "; ui->labelPosition->setText(labelMes); //设置当前行高亮 QList<QTextEdit::ExtraSelection> extraSelections; QTextEdit::ExtraSelection ext; //1. 知道当前行 ext.cursor = cursor; QBrush qBrush(Qt::lightGray); //2. 颜色 ext.format.setBackground(qBrush); //配置段属性:整行显示,没有这句话不行 ext.format.setProperty(QTextFormat::FullWidthSelection, true); //ext.format.setFontUnderline(true); //3. 设置 //把ext加到ext的容器中 extraSelections.append(ext); ui->textEdit->setExtraSelections(extraSelections); }
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QFile> #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: QFile file; Widget(QWidget *parent = nullptr); ~Widget(); void zoomIn(); void zoomOut(); bool eventFilter(QObject *watched, QEvent *event); private slots: void on_btnFileOpen_clicked(); void on_btnSave_clicked(); void on_btnClose_clicked(); void onCurrentIndexChanged(int index); void onCursorPositionChanged(); private: Ui::Widget *ui; }; #endif // WIDGET_H
;
ui->labelPosition->setText(labelMes);
//设置当前行高亮 QList<QTextEdit::ExtraSelection> extraSelections; QTextEdit::ExtraSelection ext; //1. 知道当前行 ext.cursor = cursor; QBrush qBrush(Qt::lightGray); //2. 颜色 ext.format.setBackground(qBrush); //配置段属性:整行显示,没有这句话不行 ext.format.setProperty(QTextFormat::FullWidthSelection, true); //ext.format.setFontUnderline(true); //3. 设置 //把ext加到ext的容器中 extraSelections.append(ext); ui->textEdit->setExtraSelections(extraSelections);
}
**widget.h** ```c++ #ifndef WIDGET_H #define WIDGET_H #include <QFile> #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: QFile file; Widget(QWidget *parent = nullptr); ~Widget(); void zoomIn(); void zoomOut(); bool eventFilter(QObject *watched, QEvent *event); private slots: void on_btnFileOpen_clicked(); void on_btnSave_clicked(); void on_btnClose_clicked(); void onCurrentIndexChanged(int index); void onCursorPositionChanged(); private: Ui::Widget *ui; }; #endif // WIDGET_H
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。