qt 制作记事本:自动备份,(正则表达式)搜索替换
基本的记事本编辑操作相关类:QTextEdit 正则表达式替换相关类:QRegExp #include <QCoreApplication>
#include <QRegExp>
#include <QDebug>
#include <QFile>
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<<newText;
outFile.flush();
outFile.close();
// no
// text = in.readAll();
// text.replace(QRegExp(".*12$"),"00000");
// qDebug()<<text;
return a.exec();
}
[edemon@CentOS workspace]$ cat read
sdafasd12
cdsaf12
dasgfdagsfdh12
sadfasdgasg
[edemon@CentOS workspace]$ cat read1
00000
00000
00000
sadfasdgasg
普通替换相关的类和方法 正则搜索高亮结果显示继承于 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);
}
}
貌似:自定义的highlighter只能用指针来声明对象,否则不能渲染。 普通搜索QString::indexOf(QString) void simLighter::highlightBlock(const QString &text)
{
QTextCharFormat myClassFormat;
QColor color(0,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设定备份周期。 多标签利用QTabWidget类,tab的index是从0开始的。 代码设计Action的快捷键
文件备份与恢复gif文本查找gif正则替换gif源码地址:https://github.com/theArcticOcean/notepad (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |