智慧建設(shè)網(wǎng)站正規(guī)淘寶代運(yùn)營去哪里找
一.UI界面搭建
(ui界面使用,界面布局,各控件介紹,界面大小調(diào)整)
二.信號槽機(jī)制實(shí)現(xiàn)文件的打開,保存,退出
(信號槽,QFile文件類,QTextStream類,QFileDialog文件對話框,QMessage消息對話框)
//實(shí)現(xiàn)功能 : 打開目標(biāo)文件,按照右下角ComboBox所選編碼格式打開
void Widget::on_btnopen_clicked()
{//文件內(nèi)容顯示到編輯欄中QString filename = QFileDialog::getOpenFileName(this,"打開文件","C:\\Users\\SlanderMC\\Desktop");file.setFileName(filename);file.open(QIODevice::ReadWrite|QIODevice::Text);this->setWindowTitle(filename+"----記事本");QTextStream stream(&file);switch (ui->comboBox->currentIndex()) {case 0:stream.setEncoding(QStringConverter::Utf8);break;case 1:stream.setEncoding(QStringConverter::Utf16);break;case 2:stream.setEncoding(QStringConverter::Utf32);break;case 3:stream.setEncoding(QStringConverter::Utf32BE);break;}QString str = stream.readAll();ui->textEdit->setText(str);ui->fontsizelabel->setText(QString("%1 px").arg(ui->textEdit->font().pointSize()));
}//實(shí)現(xiàn)功能 : 若已打開文件,直接保存內(nèi)容到當(dāng)前文件 若未打開,則用戶選擇路徑進(jìn)行保存
void Widget::on_btnsave_clicked()
{//編輯后的內(nèi)容保存到文件if(!file.isOpen()){QString strsave = QFileDialog::getSaveFileName(this,"保存文件","C:\\Users\\SlanderMC\\Desktop");file.setFileName(strsave);file.open(QIODevice::WriteOnly|QIODevice::Text);this->setWindowTitle(strsave+"----記事本");}QTextStream stream(&file);//按照當(dāng)前的編碼格式寫入switch (ui->comboBox->currentIndex()) {case 0:stream.setEncoding(QStringConverter::Utf8);break;case 1:stream.setEncoding(QStringConverter::Utf16);break;case 2:stream.setEncoding(QStringConverter::Utf32);break;case 3:stream.setEncoding(QStringConverter::Utf32BE);break;}stream.seek(0);stream<<ui->textEdit->toPlainText();}//實(shí)現(xiàn)功能 : 使用消息對話框,判斷用戶是否保存
//保存,丟棄(斷開連接,置空文本框),退出
void Widget::on_btnclose_clicked()
{int res = QMessageBox::warning(this,"提示","您想要保存嗎?",QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);switch (res) {case QMessageBox::Save:on_btnsave_clicked();break;case QMessageBox::Discard:ui->textEdit->clear();if(file.isOpen()){file.close();this->setWindowTitle("記事本");}break;case QMessageBox::Cancel://不做處理break;}
}
三.實(shí)現(xiàn)文件切換編碼格式功能
(QTextStream類,comboBox選擇框控件)
//實(shí)現(xiàn)功能 : 實(shí)施切換當(dāng)前文件讀取的編碼格式
void Widget::on_comboBox_currentIndexChanged(int index)
{if(file.isOpen()){int index = ui->comboBox->currentIndex();QTextStream stream(&file);switch (index) {case 0:stream.setEncoding(QStringConverter::Utf8);break;case 1:stream.setEncoding(QStringConverter::Utf16);break;case 2:stream.setEncoding(QStringConverter::Utf32);break;case 3:stream.setEncoding(QStringConverter::Utf32BE);break;}//seek切換文件光標(biāo)位置stream.seek(0);ui->textEdit->setText(stream.readAll());}
}
四.實(shí)現(xiàn)鼠標(biāo)當(dāng)前所在行高亮的功能,并在右下角顯示行列
(textEdit標(biāo)準(zhǔn)控件,ExtraSelection類(包括cursor和QTextCharFormat兩個(gè)成員))
//TextEdit的光標(biāo)移動(dòng)信號 “cursorPositionChanged” 對應(yīng)的槽函數(shù) "on_textEdit_cursorPositionChanged"
//--------實(shí)現(xiàn)功能 : 根據(jù)光標(biāo)位置在右下角顯示行列
void Widget::on_textEdit_cursorPositionChanged()
{//textCursor光標(biāo)類,返回一個(gè)可視光標(biāo)QTextCursor tc = ui->textEdit->textCursor();//獲取行列int block = tc.blockNumber()+1;int column = tc.columnNumber()+1;//字符串拼接,標(biāo)簽更改QString str = QString("第%1行,第%2列").arg(block).arg(column);ui->labelhl->setText(str);//QTextEdit::ExtraSelection 是一個(gè)在 QTextEdit 中用來表示額外的文本選擇和高亮的結(jié)構(gòu)。//設(shè)置當(dāng)前行高亮QList<QTextEdit::ExtraSelection> extralist;//ExtraSelection結(jié)構(gòu)體包括兩個(gè)成員 QTextCursor 和 QTextCharFormatQTextEdit::ExtraSelection extra;//QTextCursor 表示在文本中的一個(gè)位置或者區(qū)間,而extra.cursor = tc;//QTextCharFormat 用于定義這個(gè)區(qū)間的格式,比如背景顏色、字體,下劃線等。QTextCharFormat f;f.setBackground(QBrush(Qt::lightGray));f.setFontUnderline(true);f.setUnderlineColor(Qt::white);f.setUnderlineStyle(QTextCharFormat::WaveUnderline);//配置整行顯示,否則無效f.setProperty(QTextFormat::FullWidthSelection,true);extra.format = f;extralist.append(extra);ui->textEdit->setExtraSelections(extralist);}
五.實(shí)現(xiàn)Ctrl+O打開,Ctrl+S保存等快捷鍵功能
(QShortCut類,QKeySequence類,lambda表達(dá)式實(shí)現(xiàn)信號槽)
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);ui->textEdit->installEventFilter(this);this->setWindowIcon(QIcon(":/resourse/open2.png"));this->setWindowTitle("記事本");this->setLayout(ui->verticalLayout); //整體Widget垂直布局//快捷鍵 QT提供的QShortcut類// 并將其快捷鍵序列設(shè)置為 "Ctrl+N"實(shí)現(xiàn)的。//QKeySequence用以設(shè)置快捷鍵//綁定其activated信號連接到一個(gè)槽函數(shù)QShortcut *cutopen = new QShortcut(QKeySequence("Ctrl+O"),this);QShortcut *cutsave = new QShortcut(QKeySequence("Ctrl+S"),this);connect(cutopen,&QShortcut::activated,[=](){this->on_btnopen_clicked();});connect(cutsave,&QShortcut::activated,[=](){this->on_btnsave_clicked();});
}
六.實(shí)現(xiàn)字體放大縮小功能,實(shí)時(shí)顯示 (快捷鍵實(shí)現(xiàn))
void Widget::fontsizeup()
{QFont font = ui->textEdit->font();font.setPointSize(font.pointSize()+1);ui->textEdit->setFont(font);ui->fontsizelabel->setText(QString("%1 px").arg(font.pointSize()));
}void Widget::fontsizedown()
{QFont font = ui->textEdit->font();font.setPointSize(font.pointSize()-1);ui->textEdit->setFont(font);ui->fontsizelabel->setText(QString("%1 px").arg(font.pointSize()));
}
//設(shè)置快捷鍵,字體放大縮小QShortcut *cutsizeup = new QShortcut(QKeySequence("Ctrl+="),this);QShortcut *cutsizedown = new QShortcut(QKeySequence("Ctrl+-"),this);connect(cutsizeup,&QShortcut::activated,[=](){fontsizeup();});connect(cutsizedown,&QShortcut::activated,[=](){fontsizedown();});
七.實(shí)現(xiàn)字體放大縮小功能(Ctrl+滾輪實(shí)現(xiàn))
(QT事件,事件過濾器eventFilter,QEvent類及其子類,事件處理流程,事件處理函數(shù)重寫)
①定義一個(gè)新的QTextEdit類,重寫其事件處理函數(shù),并將原本的QTextEdit提升為此類
頭文件
#ifndef MYTEXTEDIT_H
#define MYTEXTEDIT_H#include <QTextEdit>
#include <QWidget>class mytextedit : public QTextEdit
{Q_OBJECT
public:explicit mytextedit(QWidget *parent = nullptr);void wheelEvent(QWheelEvent *e) override;void keyPressEvent(QKeyEvent *e)override;void keyReleaseEvent(QKeyEvent *e)override;
private:bool keyPress = false;
signals:};#endif // MYTEXTEDIT_H
源文件
#include "mytextedit.h"
#include<QDebug>
#include <QTextEdit>
#include <QWheelEvent>
mytextedit::mytextedit(QWidget *parent): QTextEdit{parent}
{}void mytextedit::wheelEvent(QWheelEvent *e)
{if(keyPress == true) //按下Ctrl時(shí),滑動(dòng)滾輪切換字體大小{if(e->angleDelta().y()> 0 ){zoomIn();}else{zoomOut();}e->accept();}else //沒有摁下Ctrl時(shí),默認(rèn)調(diào)動(dòng)滑塊{QTextEdit::wheelEvent(e);}
}void mytextedit::keyPressEvent(QKeyEvent *e)
{if(e->key() == Qt::Key_Control) //按下Ctrl{this->keyPress = true;}QTextEdit::keyPressEvent(e);
}void mytextedit::keyReleaseEvent(QKeyEvent *e)
{if(e->key() == Qt::Key_Control) //按下Ctrl{this->keyPress = false;}QTextEdit::keyReleaseEvent(e);
}
②.重寫事件過濾器
事件處理之前需要經(jīng)過事件過濾處理,所以也可以在事件過濾函數(shù)中對某些事件進(jìn)行攔截,比如這里的鼠標(biāo)滾輪事件。
true表示事件處理完畢,不再向下分發(fā),false表示事件繼續(xù)向下分發(fā)。
事件過濾器需要安裝在某個(gè)組件上,這里需要安裝在Widget類下的TextEdit,所以過濾器定義在Widget下,并在TextEdit上進(jìn)行安裝。
重寫eventFilter
//先前對于事件的處理都是直接重寫事件處理函數(shù),此次借用 EventFilter事件過濾器來實(shí)現(xiàn)對于指定事件的處理
//事件發(fā)生->事件過濾->事件分發(fā)(分類)->事件處理
//在過濾階段,處理邏輯寫完后,不希望事件繼續(xù)排放,返回true,表示事件已處理完畢。反之返回false,繼續(xù)向下派發(fā),由事件處理函數(shù)處理
bool Widget:: eventFilter(QObject *watched, QEvent *event)
{QKeyEvent *e = (QKeyEvent*)event;if(e->key() == Qt::Key_Control ){if(e->type() == QKeyEvent::KeyPress){qDebug()<<"key";this->isWheel = true;}else if(e->type() == QKeyEvent::KeyRelease){qDebug()<<"release";this->isWheel = false;}}QWheelEvent *e1 = (QWheelEvent*) event;if(e1->type() == QEvent::Wheel){if(this->isWheel){if(e1->angleDelta().y() >0){fontsizeup();}else if (e1->angleDelta().y() <0){fontsizedown();}return true;}else{return false;}}
}