本文整理汇总了C++中QTextLayout::setTextOption方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLayout::setTextOption方法的具体用法?C++ QTextLayout::setTextOption怎么用?C++ QTextLayout::setTextOption使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextLayout
的用法示例。
在下文中一共展示了QTextLayout::setTextOption方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: placeAnchoredFrame
void TestDocumentLayout::placeAnchoredFrame()
{
initForNewTest(QString());
MockShape *picture = new MockShape();
picture->setSize(QSizeF(100, 100));
KTextAnchor *anchor = new KTextAnchor(picture);
anchor->setOffset(QPointF(23, 45));
QTextCursor cursor(doc);
KInlineTextObjectManager *manager = new KInlineTextObjectManager();
layout->setInlineTextObjectManager(manager);
MockLayoutState *state = new MockLayoutState(doc);
layout->setLayout(state);
state->shape = shape1;
QCOMPARE(doc->begin().text().length(), 0);
manager->insertInlineObject(cursor, anchor);
QCOMPARE(doc->begin().text().length(), 1);
QCOMPARE(cursor.position(), 1);
shape1->setPosition(QPointF(300, 300));
layout->layout();
QCOMPARE(picture->parent(), shape1);
QCOMPARE(picture->position(), QPointF(23, 59.4));
cursor.setPosition(0);
cursor.insertText("foo"); // moves my anchors slightly to the right/down and gives line height
layout->layout();
QCOMPARE(picture->parent(), shape1);
QPointF newPos = picture->position();
QVERIFY(newPos.x() > 23);
QVERIFY(newPos.y() > 45); // it adds the baseline now
cursor.movePosition(QTextCursor::End);
cursor.insertText("\nNew Line\nAnd another");
layout->layout();
QCOMPARE(picture->position(), newPos);
QTextLayout *firstLineLayout = doc->begin().layout();
QTextOption option = firstLineLayout->textOption();
option.setAlignment(Qt::AlignHCenter);
firstLineLayout->setTextOption(option);
layout->layout();
QTextLine first = doc->begin().layout()->lineAt(0);
QVERIFY(first.isValid());
QVERIFY(first.naturalTextRect().x() > 10);
newPos.setX(newPos.x() + first.naturalTextRect().x()); // text is moved due to alignment
QCOMPARE(picture->position(), newPos);
anchor->setOffset(QPointF());
anchor->setAlignment(KTextAnchor::Left);
anchor->setAlignment(KTextAnchor::TopOfParagraph);
layout->layout();
// image is 100 wide, now centered in a parent of 200 so X = 50
QCOMPARE(picture->position(), QPointF(50, 0));
}
示例3: viewItemTextSize
static QSize viewItemTextSize ( const QStyleOptionViewItemV4 *option )
{
QStyle *style = option->widget ? option->widget->style() : QApplication::style();
QTextOption textOption;
textOption.setWrapMode ( QTextOption::WrapAtWordBoundaryOrAnywhere );
QTextLayout textLayout;
textLayout.setTextOption ( textOption );
textLayout.setFont ( option->font );
textLayout.setText ( option->text );
const int textMargin = style->pixelMetric ( QStyle::PM_FocusFrameHMargin, option, option->widget ) + 1;
QRect bounds ( 0,0,100 - 2*textMargin,600 );
qreal height = 0, widthUsed = 0;
viewItemTextLayout ( textLayout, bounds.width(), height, widthUsed );
const QSize size ( qCeil ( widthUsed ), qCeil ( height ) );
return QSize ( size.width() + 2 * textMargin, size.height() );
}
示例4: 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);
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();
}
}
示例5: layoutBlock
void TextDocumentLayout::layoutBlock(const QTextBlock &block)
{
QTextDocument *doc = document();
qreal margin = doc->documentMargin();
qreal blockMaximumWidth = 0;
qreal height = 0;
QTextLayout *tl = block.layout();
QTextOption option = doc->defaultTextOption();
tl->setTextOption(option);
int extraMargin = 0;
if (option.flags() & QTextOption::AddSpaceForLineAndParagraphSeparators) {
QFontMetrics fm(block.charFormat().font());
extraMargin += fm.width(QChar(0x21B5));
}
tl->beginLayout();
qreal availableWidth = d->width;
if (availableWidth <= 0) {
availableWidth = qreal(INT_MAX); // similar to text edit with pageSize.width == 0
}
availableWidth -= 2*margin + extraMargin;
qreal indentMargin = 0;
while (1) {
QTextLine line = tl->createLine();
if (!line.isValid())
break;
line.setLeadingIncluded(true);
line.setLineWidth(availableWidth - indentMargin);
line.setPosition(QPointF(margin + indentMargin, height));
if(!height) //enter only in the first iteration
{
indentMargin = indentWidth(block);
}
height += line.height();
blockMaximumWidth = qMax(blockMaximumWidth, line.naturalTextWidth() + 2*margin);
}
tl->endLayout();
int previousLineCount = doc->lineCount();
const_cast<QTextBlock&>(block).setLineCount(block.isVisible() ? tl->lineCount() : 0);
int lineCount = doc->lineCount();
bool emitDocumentSizeChanged = previousLineCount != lineCount;
if (blockMaximumWidth > d->maximumWidth) {
// new longest line
d->maximumWidth = blockMaximumWidth;
d->maximumWidthBlockNumber = block.blockNumber();
emitDocumentSizeChanged = true;
} else if (block.blockNumber() == d->maximumWidthBlockNumber && blockMaximumWidth < d->maximumWidth) {
// longest line shrinking
QTextBlock b = doc->firstBlock();
d->maximumWidth = 0;
QTextBlock maximumBlock;
while (b.isValid()) {
qreal blockMaximumWidth = blockWidth(b);
if (blockMaximumWidth > d->maximumWidth) {
d->maximumWidth = blockMaximumWidth;
maximumBlock = b;
}
b = b.next();
}
if (maximumBlock.isValid()) {
d->maximumWidthBlockNumber = maximumBlock.blockNumber();
emitDocumentSizeChanged = true;
}
}
if (emitDocumentSizeChanged)// && !d->blockDocumentSizeChanged)
emit documentSizeChanged(documentSize());
emit updateBlock(block);
}
示例6: paint
//.........这里部分代码省略.........
textHighlightRect.adjust ( 0,iconSize + 5,0,0 );
// draw background
{
QSize textSize = viewItemTextSize ( &opt );
QPalette::ColorGroup cg;
QStyleOptionViewItemV4 opt2(opt);
if((opt.widget && opt.widget->isEnabled()) || (opt.state & QStyle::State_Enabled))
{
if(! ( opt.state & QStyle::State_Active ))
cg = QPalette::Inactive;
else
cg = QPalette::Normal;
}
else
{
cg = QPalette::Disabled;
}
opt2.palette.setCurrentColorGroup(cg);
// fill in background, if any
if ( opt.backgroundBrush.style() != Qt::NoBrush )
{
QPointF oldBO = painter->brushOrigin();
painter->setBrushOrigin ( opt.rect.topLeft() );
painter->fillRect ( opt.rect, opt.backgroundBrush );
painter->setBrushOrigin ( oldBO );
}
if ( opt.showDecorationSelected )
{
drawSelectionRect(painter,opt2, opt.rect);
drawFocusRect(painter,opt2, opt.rect);
//painter->fillRect ( opt.rect, opt.palette.brush ( cg, QPalette::Highlight ) );
}
else
{
//if ( opt.state & QStyle::State_Selected )
{
//QRect textRect = subElementRect ( QStyle::SE_ItemViewItemText, opt, opt.widget );
//painter->fillRect ( textHighlightRect, opt.palette.brush ( cg, QPalette::Highlight ) );
drawSelectionRect(painter,opt2, textHighlightRect);
drawFocusRect(painter,opt2, textHighlightRect);
}
}
}
// draw the icon
{
QIcon::Mode mode = QIcon::Normal;
if ( ! ( opt.state & QStyle::State_Enabled ) )
mode = QIcon::Disabled;
else if ( opt.state & QStyle::State_Selected )
mode = QIcon::Selected;
QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
iconbox.setHeight ( iconSize );
opt.icon.paint ( painter, iconbox, Qt::AlignCenter, mode, state );
}
// set the text colors
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if ( cg == QPalette::Normal && ! ( opt.state & QStyle::State_Active ) )
cg = QPalette::Inactive;
if ( opt.state & QStyle::State_Selected )
{
painter->setPen ( opt.palette.color ( cg, QPalette::HighlightedText ) );
}
else
{
painter->setPen ( opt.palette.color ( cg, QPalette::Text ) );
}
// draw the text
QTextOption textOption;
textOption.setWrapMode ( QTextOption::WrapAtWordBoundaryOrAnywhere );
textOption.setTextDirection ( opt.direction );
textOption.setAlignment ( QStyle::visualAlignment ( opt.direction, opt.displayAlignment ) );
QTextLayout textLayout;
textLayout.setTextOption ( textOption );
textLayout.setFont ( opt.font );
textLayout.setText ( opt.text );
qreal width, height;
viewItemTextLayout ( textLayout, iconbox.width(), height, width );
const int lineCount = textLayout.lineCount();
const QRect layoutRect = QStyle::alignedRect ( opt.direction, opt.displayAlignment, QSize ( iconbox.width(), int ( height ) ), textRect );
const QPointF position = layoutRect.topLeft();
for ( int i = 0; i < lineCount; ++i )
{
const QTextLine line = textLayout.lineAt ( i );
line.draw ( painter, position );
}
painter->restore();
}
示例7: drawDisplay
// copied from QItemDelegate to be able to add the 'format' parameter
void HighlightingItemDelegate::drawDisplay(QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect, const QString &text,
const QVector<QTextLayout::FormatRange> &format) const
{
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
cg = QPalette::Inactive;
if (option.state & QStyle::State_Selected) {
painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight));
painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setPen(option.palette.color(cg, QPalette::Text));
}
if (text.isEmpty())
return;
if (option.state & QStyle::State_Editing) {
painter->save();
painter->setPen(option.palette.color(cg, QPalette::Text));
painter->drawRect(rect.adjusted(0, 0, -1, -1));
painter->restore();
}
const QStyleOptionViewItem opt = option;
const QWidget *widget = option.widget;
QStyle *style = widget ? widget->style() : QApplication::style();
const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
const bool wrapText = opt.features & QStyleOptionViewItem::WrapText;
QTextOption textOption;
textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
textOption.setTextDirection(option.direction);
textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment));
QTextLayout textLayout;
textLayout.setTextOption(textOption);
textLayout.setFont(option.font);
textLayout.setText(replaceNewLine(text));
QSizeF textLayoutSize = doTextLayout(&textLayout, textRect.width());
if (textRect.width() < textLayoutSize.width()
|| textRect.height() < textLayoutSize.height()) {
QString elided;
int start = 0;
int end = text.indexOf(QChar::LineSeparator, start);
if (end == -1) {
elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
} else {
while (end != -1) {
elided += option.fontMetrics.elidedText(text.mid(start, end - start),
option.textElideMode, textRect.width());
elided += QChar::LineSeparator;
start = end + 1;
end = text.indexOf(QChar::LineSeparator, start);
}
// let's add the last line (after the last QChar::LineSeparator)
elided += option.fontMetrics.elidedText(text.mid(start),
option.textElideMode, textRect.width());
}
textLayout.setText(elided);
textLayoutSize = doTextLayout(&textLayout, textRect.width());
}
const QSize layoutSize(textRect.width(), int(textLayoutSize.height()));
const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment,
layoutSize, textRect);
// if we still overflow even after eliding the text, enable clipping
if (!hasClipping() && (textRect.width() < textLayoutSize.width()
|| textRect.height() < textLayoutSize.height())) {
painter->save();
painter->setClipRect(layoutRect);
textLayout.draw(painter, layoutRect.topLeft(), format, layoutRect);
painter->restore();
} else {
textLayout.draw(painter, layoutRect.topLeft(), format, layoutRect);
}
}