我在 Qt Creator 中收到此警告:
不要创建临时的 QRegularExpression 对象。使用静态 QRegularExpression 对象代替 [clazy-use-static-qregularexpression]
这是关于下面的代码片段:
QRegularExpression re("SEARCHING...",QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = re.match(frame);
if (match.hasMatch()) {
这对我来说并不明显,我应该如何使用
QRegularExpression
来代替?
这是一条愚蠢的警告消息,您可以在此处找到其描述。这只是表明您不想每次输入该函数时都重新创建 QRegularExpression,因为表达式始终相同。因此,这样做会起作用,并提高代码的性能:
static QRegularExpression re("SEARCHING...", QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = re.match(frame);
if (match.hasMatch()) {
我的解决方案是使用 Q_GLOBAL_STATIC_WITH_ARGS
我在使用它的源文件中定义了全局表达式。
Q_GLOBAL_STATIC_WITH_ARGS(QRegularExpression, regExp, {"SEARCHING..."});
我这样使用:
auto match = regExp->match(frame);
if (match.hasMatch()) {