请教 Qt 中两个 QString 用%连接是什么意思呢?

2021-02-24 20:36:35 +08:00
 sarlanori

在编译 Log4Qt 时报错:

.\log4qt\dailyfileappender.cpp:109: error: invalid operands to binary expression ('QString' and 'QString')

报错的代码如下:

const QRegularExpression creationDateExtractor(
                fi.baseName() % QStringLiteral("(.*)") % QStringLiteral(".") % fi.completeSuffix());

类似的写法该文件中还有多处,比如:

QString DailyFileAppender::appendDateToFilename() const
{
    QFileInfo fi(mOriginalFilename);
    return fi.absolutePath() % QStringLiteral("/") % fi.baseName() %  mLastDate.toString(mDatePattern) % QStringLiteral(".") % fi.completeSuffix();
}

Qt 半路出家,不是太熟悉,谢谢大家了!

684 次点击
所在节点    问与答
2 条回复
xfcy
2021-02-24 20:57:44 +08:00
QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.

文档中的原话。

https://doc.qt.io/qt-5.15/qstring.html
sarlanori
2021-02-24 21:26:02 +08:00
@xfcy #1 学习了,谢谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/755964

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX