【github notepad】QT 自动备份,(正则表达式)搜索替换

网友投稿 639 2022-08-27

【github notepad】QT 自动备份,(正则表达式)搜索替换

【github notepad】QT 自动备份,(正则表达式)搜索替换

本文记录使用qt制作一个记事本的一些问题及其解决方案。

基本的记事本编辑操作

相关类:QTextEdit

正则表达式替换

相关类:QRegExp 注:在正则替换时,使用文件流readLine得到的字符串才能用于正则匹配,QTextStream::readAll()和QTextEdit::toPlainText()的得到的字符串消除了​​​^ $​​​。​​.*A$​​​的正则表达式找不到以A结尾的行。 例如:

#include #include #include #include int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QString path = "/home/edemon/workspace/read"; QFile inputFile(path); if(!inputFile.open(QIODevice::ReadOnly)){ qDebug()<<"read open failed."; return 1; } QTextStream in(&inputFile); QString text, newText=""; // ok text=in.readLine(); while(!text.isEmpty()){ text.replace(QRegExp(".*12$"),"00000"); newText = newText+text+"\n"; text=in.readLine(); } QFile outFile(path+"1"); if(!outFile.open(QIODevice::WriteOnly)){ qDebug()<<"write open failed."; return 1; } QTextStream out(&outFile); out<

[edemon@CentOS workspace]$ cat readsdafasd12cdsaf12dasgfdagsfdh12sadfasdgasg[edemon@CentOS workspace]$ cat read1000000000000000

普通替换

相关的类和方法 QTextEdit::find(); QTextEdit::textCursor(); QTextEdit::insertText(); QTextEdit::movePosition(QTextCursor::Start);

正则搜索高亮结果显示

继承于 QSyntaxHighlighter的自定义类 主要方法:

void MyHighlighter::highlightBlock(const QString &text){ QTextCharFormat myClassFormat; QColor color(0,0,100,100); myClassFormat.setBackground(color); QString pattern = "regular"; QRegExp expression(pattern); int index = text.indexOf(expression); while (index >= 0) { int length = expression.matchedLength(); setFormat(index, length, myClassFormat); index = text.indexOf(expression, index + length); }}

普通搜索

QString::indexOf(QString)

void simLighter::highlightBlock(const QString &text){ QTextCharFormat myClassFormat; QColor color(0,0,100,100); myClassFormat.setBackground(color); int index = text.indexOf(pattern); while (index >= 0) { int length = pattern.length(); //expression.matchedLength(); setFormat(index, length, myClassFormat); index = text.indexOf(pattern, index + length); }}

当前的文本备份

QTimer设定备份周期。 用QFIle,QTextStream 等类将文本写入磁盘。

多标签

利用QTabWidget类,tab的index是从0开始的。

代码设计Action的快捷键

找按键表​​​New->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); // 0x04000000 + 0x4e​​字符串转换​​​New->setShortcut(QKeySequence(tr("Ctrl+N")));​​

文件备份与恢复gif

文本查找gif

正则替换gif

源码地址:​​https://github.com/theArcticOcean/notepad​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:1>&2, 2>&1, &>
下一篇:QT enVocabulary工程打包问题记录
相关文章

 发表评论

暂时没有评论,来抢沙发吧~