sarlanori
V2EX  ›  问与答

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

  •  
  •   sarlanori · Feb 24, 2021 · 1380 views
    This topic created in 2077 days ago, the information mentioned may be changed or developed.

    在编译 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 replies  •  2021-02-24 21:26:02 +08:00
    xfcy
    xfcy
    Feb 24, 2021
    1
    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
    sarlanori
    OP
    Feb 24, 2021
    2
    @xfcy #1 学习了,谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3191 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:44 · PVG 18:44 · LAX 02:44 · JFK 05:44
    ♥ Do have faith in what you're doing.