当前位置: 首页>>代码示例>>C++>>正文


C++ QTextLayout::setCacheEnabled方法代码示例

本文整理汇总了C++中QTextLayout::setCacheEnabled方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLayout::setCacheEnabled方法的具体用法?C++ QTextLayout::setCacheEnabled怎么用?C++ QTextLayout::setCacheEnabled使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QTextLayout的用法示例。


在下文中一共展示了QTextLayout::setCacheEnabled方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: calculateSingleLineLayout

/// Calculate single line layout for given text with specified font.
/// This function automatically add ellipsis text if the naural width
/// is larger than given region.
/// \layout The result layout.
/// \font The font object.
/// \string The text needs to be layouted.
/// \align Alignment.
/// \rect The region.
/// \ellipsis Add ellipse to left or right if necessary.
/// Returns the line height.
int calculateSingleLineLayout(QTextLayout & layout,
                              const QFont & font,
                              const QString & string,
                              const Qt::Alignment align,
                              const QRect & rect,
                              const Qt::TextElideMode ellipsis)
{
    QFontMetrics fm(font);

    // Check if we need to add ellipsis.
    QString result_string = string;
    int width = ellipsisText(string, fm, rect.width(),
                             ellipsis, result_string);

    // Construct the layout.
    layout.clearLayout();
    layout.setCacheEnabled(false);
    layout.setText(result_string);
    layout.setFont(font);
    layout.beginLayout();
    QTextLine line = layout.createLine();
    if (line.isValid())
    {
        line.setLineWidth(rect.width());
    }
    layout.endLayout();

    // Calculate the position.
    int x = rect.left();
    int y = rect.top();
    int h = static_cast<int>(line.height());

    if (align & Qt::AlignHCenter)
    {
        x = ((rect.width() - width) >> 1) + x;
    }
开发者ID:HeryLong,项目名称:booxsdk,代码行数:46,代码来源:text_layout.cpp

示例2: paintText

void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p)
{
    bool preferRichText = textFormat == Qt::RichText
                          || (textFormat == Qt::AutoText && Qt::mightBeRichText(text));

    if (!preferRichText) {
        QTextLayout textLayout;
        textLayout.setText(text);
        textLayout.setFont(font);
        textLayout.setTextOption(textOption);
        textLayout.setCacheEnabled(true);

        qreal leading = QFontMetricsF(font).leading();
        qreal height = -leading;

        textLayout.beginLayout();
        while (1) {
            QTextLine line = textLayout.createLine();
            if (!line.isValid())
                break;

            if (textWidth >= 0.0)
                line.setLineWidth(textWidth);
            height += leading;
            line.setPosition(QPointF(0.0, height));
            height += line.height();
        }
        textLayout.endLayout();

        actualSize = textLayout.boundingRect().size();
        textLayout.draw(p, topLeftPosition);
    } else {
        QTextDocument document;
#ifndef QT_NO_CSSPARSER
        QColor color = p->pen().color();
        document.setDefaultStyleSheet(QString::fromLatin1("body { color: #%1%2%3 }")
                                      .arg(QString::number(color.red(), 16), 2, QLatin1Char('0'))
                                      .arg(QString::number(color.green(), 16), 2, QLatin1Char('0'))
                                      .arg(QString::number(color.blue(), 16), 2, QLatin1Char('0')));
#endif
        document.setDefaultFont(font);
        document.setDocumentMargin(0.0);
#ifndef QT_NO_TEXTHTMLPARSER
        document.setHtml(text);
#else
        document.setPlainText(text);
#endif
        if (textWidth >= 0.0)
            document.setTextWidth(textWidth);
        else
            document.adjustSize();
        document.setDefaultTextOption(textOption);

        p->save();
        p->translate(topLeftPosition);
        QAbstractTextDocumentLayout::PaintContext ctx;
        ctx.palette.setColor(QPalette::Text, p->pen().color());
        document.documentLayout()->draw(p, ctx);
        p->restore();

        if (textWidth >= 0.0)
            document.adjustSize(); // Find optimal size

        actualSize = document.size();
    }
}
开发者ID:Drakey83,项目名称:steamlink-sdk,代码行数:66,代码来源:qstatictext.cpp


注:本文中的QTextLayout::setCacheEnabled方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。