Replace (MIME)=(*)
-
Set default app: ~/.config/mimeapps.list
MIME and desktop is dynamic, get by FileManager openwith interface.
Matchimage/jpg=deepin-image-viewer.desktop
Relace to
image/jpg=HTYIV.desktop
QString MIME = "image/jpg"; QString desktop = "/usr/share/applications/HTYIV.desktop"; QString default_apps = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/mimeapps.list"; QFile *file = new QFile; file->setFileName(default_apps); QString s=""; bool ok = file->open(QIODevice::ReadOnly); if (ok) { QTextStream TS(file); s = TS.readAll(); file->close(); } file->setFileName(default_apps); ok = file->open(QFile::WriteOnly); if (ok) { QFileInfo FI(desktop); QTextStream TS(file); s.replace(QRegularExpression(MIME + "=*\n"), MIME + "=" + FI.fileName()); TS << s; file->close(); }
-
Hi,
Since there's no question, are you asking why the regular expression you are using to replace =* does not work ?
That's because * has a specific meaning in regular expressions. It means 0 or more times the preceding expression. So if you want to detect in a string you need to escape it.
On a side note, you should move to QRegularExpression as QRegExp has been deprecated in Qt 5 and removed in Qt 6.
-
@SGaist said in Replace (MIME)=(*):
Hi,
Since there's no question, are you asking why the regular expression you are using to replace =* does not work ?
That's because * has a specific meaning in regular expressions. It means 0 or more times the preceding expression. So if you want to detect in a string you need to escape it.
On a side note, you should move to QRegularExpression as QRegExp has been deprecated in Qt 5 and removed in Qt 6.
Done !
//s.replace(QRegularExpression(MIME + "=*\n"), MIME + "=" + FI.fileName()); s.replace(QRegularExpression(MIME + "=.*\n"), MIME + "=" + FI.fileName() + "\n");