PDA

View Full Version : How could I revert back the escape char. in QLineEdit



jemyzhang
23rd May 2015, 04:55
Strings link "\n" "\t" and etc. will be input in the QLineEdit. "\\n" will be got from QLineEdit.text(), but I want to revert it back to "\n".

One simple way is to use the replace function, QString.replace("\\n", "\n"), but it is only limited to some of the escape sequence.

Escape sequence as "\x1b" can not be simply processed by replace function.

Is there any way if I want to process all the escape sequence cases which are input from the QLineEdit?

ChrisW67
23rd May 2015, 11:11
One option is to iterate over the string with a regular expression:


QString value = "\\x5cx21\\x21def\\x21ghi\\x23";
const QRegExp re("\\\\x([0-9a-f]{2})");
int pos = 0;
while ((pos = re.indexIn(value, pos)) != -1) {
bool ok;
int v = re.cap(1).toInt(&ok, 16);
value = value.replace(pos, 4, QChar(v));
++pos;
}

jemyzhang
23rd May 2015, 11:18
One option is to iterate over the string with a regular expression:


QString value = "\\x5cx21\\x21def\\x21ghi\\x23";
const QRegExp re("\\\\x([0-9a-f]{2})");
int pos = 0;
while ((pos = re.indexIn(value, pos)) != -1) {
bool ok;
int v = re.cap(1).toInt(&ok, 16);
value = value.replace(pos, 4, QChar(v));
++pos;
}


Thanks for your suggestion. But in this way, maybe I also have to deal with the Oct.mode as \033...