sarlanori
V2EX  ›  问与答

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

  •  
  •   sarlanori · Feb 24, 2021 · 1174 views
    This topic created in 1972 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
        1
    xfcy  
       Feb 24, 2021
    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
       Feb 24, 2021
    @xfcy #1 学习了,谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5590 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 91ms · UTC 07:37 · PVG 15:37 · LAX 00:37 · JFK 03:37
    ♥ Do have faith in what you're doing.