本文整理汇总了C++中QTextLayout::font方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLayout::font方法的具体用法?C++ QTextLayout::font怎么用?C++ QTextLayout::font使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextLayout
的用法示例。
在下文中一共展示了QTextLayout::font方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: layoutText
void YTDelegate::layoutText(QTextLayout& textLayout, QString text, QSize constraint) const
{
QTextOption textOption(Qt::AlignJustify);
textLayout.setTextOption(textOption);
textLayout.setText(text);
textLayout.beginLayout();
int lHeight = 0;
while(true){
QTextLine line = textLayout.createLine();
if(!line.isValid())
break;
line.setLineWidth(constraint.width());
line.setPosition(QPointF(0, lHeight));
if(lHeight + line.height() > constraint.height())
{
QTextLine lastLine = textLayout.lineAt(textLayout.lineCount() - 2);
QString lastString = text.mid(lastLine.textStart());
QFontMetrics fm(textLayout.font());
text.chop(lastString.length());
text += fm.elidedText(lastString, Qt::ElideRight, constraint.width()-1);
textLayout.endLayout();
layoutText(textLayout, text, constraint);
return;
}
lHeight += line.height();
lHeight += line.leading();
}
textLayout.endLayout();
}
示例2: layoutText
void TextLabel::layoutText(QTextLayout &layout, const QString &text, const QSize &constraints)
{
QFontMetrics metrics(layout.font());
int leading = metrics.leading();
int height = 0;
int maxWidth = constraints.width();
int widthUsed = 0;
int lineSpacing = metrics.lineSpacing();
QTextLine line;
layout.setText(text);
layout.beginLayout();
while ((line = layout.createLine()).isValid()) {
height += leading;
// Make the last line that will fit infinitely long.
// drawTextLayout() will handle this by fading the line out
// if it won't fit in the constraints.
if (height + 2 * lineSpacing > constraints.height()) {
line.setPosition(QPoint(0, height));
break;
}
line.setLineWidth(maxWidth);
line.setPosition(QPoint(0, height));
height += int(line.height());
widthUsed = int(qMax(qreal(widthUsed), line.naturalTextWidth()));
}
layout.endLayout();
}
示例3: drawTextLayout
void TextLabel::drawTextLayout(QPainter *painter, const QTextLayout &layout, const QRect &rect)
{
if (rect.width() < 1 || rect.height() < 1) {
return;
}
QPixmap pixmap(rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.setPen(painter->pen());
// Create the alpha gradient for the fade out effect
QLinearGradient alphaGradient(0, 0, 1, 0);
alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
if (layout.textOption().textDirection() == Qt::LeftToRight) {
alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
} else {
alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
}
QFontMetrics fm(layout.font());
int textHeight = layout.lineCount() * fm.lineSpacing();
QPointF position(0, (rect.height() - textHeight) / 2);
QList<QRect> fadeRects;
int fadeWidth = 30;
// Draw each line in the layout
for (int i = 0; i < layout.lineCount(); i++) {
QTextLine line = layout.lineAt(i);
line.draw(&p, position);
// Add a fade out rect to the list if the line is too long
if (line.naturalTextWidth() > rect.width())
{
int x = int(qMin(line.naturalTextWidth(), (qreal)pixmap.width())) - fadeWidth;
int y = int(line.position().y() + position.y());
QRect r = QStyle::visualRect(layout.textOption().textDirection(), pixmap.rect(),
QRect(x, y, fadeWidth, int(line.height())));
fadeRects.append(r);
}
}
// Reduce the alpha in each fade out rect using the alpha gradient
if (!fadeRects.isEmpty()) {
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
foreach (const QRect &rect, fadeRects) {
p.fillRect(rect, alphaGradient);
}
示例4: drawHightlightText
void DTPatientListDelegate::drawHightlightText(QPainter *painter,
QString text,
QString substring,
QRect pos,
Qt::CaseSensitivity cs,
bool boldFont,
QColor highlight) const
{
if (!painter || (text.length() == 0))
return;
painter->save();
QVector<QTextLayout::FormatRange> selections;
QTextLayout textLayout;
textLayout.setText(text);
if (substring.length() != 0)
{
int idx = -1;
int start = 0;
while (1)
{
idx = text.indexOf(substring, start,cs);
if (idx == -1)
break;
QTextLayout::FormatRange* range = new QTextLayout::FormatRange;
range->start = idx;
range->length = substring.length();
range->format.setBackground(QBrush(QColor(highlight)));
selections.append(*range);
start += substring.length();
}
}
if (boldFont)
{
QFont f = textLayout.font();
f.setBold(true);
textLayout.setFont(f);
}
int leading = painter->fontMetrics().leading();
qreal height = 0;
textLayout.beginLayout();
while (1) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
line.setLineWidth(pos.width());
height += leading;
line.setPosition(QPointF(0, height));
height += line.height();
}
textLayout.endLayout();
textLayout.draw(painter, pos.topLeft(), selections);
painter->restore();
}