本文整理汇总了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;
}
示例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();
}
}