V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sarlanori
V2EX  ›  问与答

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

  •  
  •   sarlanori · 2021-02-24 20:36:35 +08:00 · 663 次点击
    这是一个创建于 1154 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在编译 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 半路出家,不是太熟悉,谢谢大家了!

    2 条回复    2021-02-24 21:26:02 +08:00
    xfcy
        1
    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
        2
    sarlanori  
    OP
       2021-02-24 21:26:02 +08:00
    @xfcy #1 学习了,谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5443 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:44 · PVG 14:44 · LAX 23:44 · JFK 02:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.