sarlanori
V2EX  ›  问与答

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

  •  
  •   sarlanori · Feb 24, 2021 · 1121 views
    This topic created in 1932 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   ·   887 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 20:55 · PVG 04:55 · LAX 13:55 · JFK 16:55
    ♥ Do have faith in what you're doing.