本文整理汇总了C++中QTextDocument类的典型用法代码示例。如果您正苦于以下问题:C++ QTextDocument类的具体用法?C++ QTextDocument怎么用?C++ QTextDocument使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QTextDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: QTextDocument
void WidgetChatInput::on_toolButtonSend_clicked()
{
if (!textEditInput->document()->isEmpty())
{
if (textEditInput->document()->lineCount() > 1)
{
QStringList lineList = textEditInput->document()->toHtml().split("\n");
for(int i = 4; i < lineList.size(); i++)
{
QTextDocument *line = new QTextDocument();
line->setHtml(lineList.at(i));
emit messageSent(line);
}
} else {
emit messageSent(textEditInput->document());
}
QTextCharFormat oldFormat = textEditInput->currentCharFormat();
textEditInput->document()->clear();
textEditInput->setCurrentCharFormat(oldFormat);
}
}
示例2: if
void
PlaylistLargeItemDelegate::drawRichText( QPainter* painter, const QRect& rect, int flags, QTextDocument& text ) const
{
text.setPageSize( QSize( rect.width(), QWIDGETSIZE_MAX ) );
QAbstractTextDocumentLayout* layout = text.documentLayout();
const int height = qRound( layout->documentSize().height() );
int y = rect.y();
if ( flags & Qt::AlignBottom )
y += ( rect.height() - height );
else if ( flags & Qt::AlignVCenter )
y += ( rect.height() - height ) / 2;
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor( QPalette::Text, painter->pen().color() );
painter->save();
painter->translate( rect.x(), y );
layout->draw( painter, context );
painter->restore();
}
示例3: shouldInsertNewline
bool MatchingText::shouldInsertNewline(const QTextCursor &tc) const
{
QTextDocument *doc = tc.document();
int pos = tc.selectionEnd();
// count the number of empty lines.
int newlines = 0;
for (int e = doc->characterCount(); pos != e; ++pos) {
const QChar ch = doc->characterAt(pos);
if (! ch.isSpace())
break;
else if (ch == QChar::ParagraphSeparator)
++newlines;
}
if (newlines <= 1 && doc->characterAt(pos) != QLatin1Char('}'))
return true;
return false;
}
示例4: initStyleOption
void ProcessItemDelegate::paint(QPainter * painter,
const QStyleOptionViewItem &option, const QModelIndex & index) const
{
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
painter->save();
painter->setClipRect(opt.rect);
// Draw the background.
const QWidget * widget = opt.widget;
QStyle * style = widget ? widget->style() : QApplication::style();
style->proxy()->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, widget);
QRect iconRect = style->subElementRect(QStyle::SE_ItemViewItemDecoration, &opt,
widget);
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, widget);
// 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;
opt.icon.paint(painter, iconRect, opt.decorationAlignment, mode, state);
// Draw the text.
QTextDocument doc;
doc.setHtml(opt.text);
doc.setDocumentMargin(2);
painter->translate(textRect.topLeft());
doc.drawContents(painter);
painter->restore();
}
示例5: drawHtmlLine
void drawHtmlLine(QPainter *painter, const QFont font, QRect rect, QString text, bool multiline,
bool leftAligned) {
if(!painter){return;}
painter->save();
QTextDocument displayDoc;
setUpDisplayDoc(displayDoc, font);
if(!leftAligned){
text = QString("<div align=\"right\">") + text + "</div>";
}
displayDoc.setHtml(text);
//multiline == false - Normally
if(multiline){
displayDoc.setTextWidth(rect.width());
} else {displayDoc.setTextWidth(-1);}
painter->translate(rect.topLeft());
if(multiline){ displayDoc.adjustSize(); }
rect.moveTopLeft(QPoint(0,0));
displayDoc.drawContents(painter, rect);
painter->restore();
}
示例6: re
void BaseEditor::findFirstOccurrance(const QString &text, QTextDocument::FindFlags qff,
bool isRE, bool init, bool isSetTextCusor)
{
if (!finded)
return;
QRegExp re(text);
QTextDocument *doc = document();
QTextCursor currentCursor = textCursor();
QTextCursor firstCursor;
QTextEdit::ExtraSelection es;
if(!init || prevFindCursor.isNull())
{
QTextCursor startCursor;
if(qff&QTextDocument::FindBackward && !prevFindCursor.isNull())
{
prevFindCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor,
abs(prevFindCursor.selectionStart()-prevFindCursor.selectionEnd()));
}
if(prevFindCursor.isNull())
startCursor = currentCursor;
else
startCursor = prevFindCursor;
firstCursor = isRE ? doc->find(re, startCursor, qff):
doc->find(text, startCursor, qff);
} else {
firstCursor = isRE ? doc->find(re, prevFindCursor.selectionStart(), qff):
doc->find(text, prevFindCursor.selectionStart(), qff);
}
if(firstCursor.isNull())
{
QTextCursor wholeCursor(doc);
if(qff & QTextDocument::FindBackward)
wholeCursor.movePosition(QTextCursor::End);
firstCursor = isRE ? doc->find(re, wholeCursor, qff):
doc->find(text, wholeCursor, qff);
}
if(firstCursor.isNull())
{
prevFindCursor = firstCursor;
return;
}
es.cursor = firstCursor;
QTextCharFormat f;
f.setBackground(Qt::blue);
f.setForeground(Qt::white);
es.format = f;
currentFindSelection.clear();
currentFindSelection.append(es);
prevFindCursor = firstCursor;
firstCursor.clearSelection();
if(isSetTextCusor)
setTextCursor(firstCursor);
ensureCursorVisible();
updateExtraSelection();
}
示例7: paintSection
void paintSection( QPainter * painter, const QRect& rect, int logicalIndex ) const override {
if ( rect.isValid() ) {
if ( logicalIndex > 0 ) {
QStyleOptionHeader op;
initStyleOption(&op);
op.text = "";
op.rect = rect;
op.textAlignment = Qt::AlignVCenter | Qt::AlignHCenter;
// draw the section
style()->drawControl( QStyle::CE_Header, &op, painter, this );
// html painting
painter->save();
QRect textRect = style()->subElementRect( QStyle::SE_HeaderLabel, &op, this );
painter->translate( textRect.topLeft() );
QTextDocument doc;
doc.setTextWidth( textRect.width() );
doc.setDefaultTextOption( QTextOption( Qt::AlignHCenter ) );
doc.setDocumentMargin(0);
doc.setHtml( model()->headerData( logicalIndex, Qt::Horizontal ).toString() );
doc.drawContents( painter, QRect( QPoint( 0, 0 ), textRect.size() ) );
painter->restore();
} else {
QHeaderView::paintSection( painter, rect, logicalIndex );
}
}
}
示例8: initStyleOption
void EventItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt, index);
QVariant value = index.data();
QBrush bgBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
QBrush fgBrush = qvariant_cast<QBrush>(index.data(Qt::ForegroundRole));
painter->setClipRect( opt.rect );
painter->setBackgroundMode(Qt::OpaqueMode);
painter->setBackground(Qt::transparent);
painter->setBrush(bgBrush);
if (bgBrush.style() != Qt::NoBrush) {
QPen bgPen;
bgPen.setColor(bgBrush.color().darker(250));
bgPen.setStyle(Qt::SolidLine);
bgPen.setWidth(1);
painter->setPen(bgPen);
painter->drawRoundedRect(opt.rect.x(), opt.rect.y(), opt.rect.width() - bgPen.width(), opt.rect.height() - bgPen.width(), 3.0, 3.0);
}
QTextDocument doc;
doc.setDocumentMargin(3);
doc.setDefaultStyleSheet("* {color: " + fgBrush.color().name() + ";}");
doc.setHtml("<html><qt></head><meta name=\"qrichtext\" content=\"1\" />" + displayText(value, QLocale::system()) + "</qt></html>");
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize( opt.rect.size());
painter->translate(opt.rect.x(), opt.rect.y());
doc.documentLayout()->draw(painter, context);
painter->restore();
}
示例9: findNaturalSize
QSize RichTextRenderer::findNaturalSize(int atWidth)
{
QTextDocument doc;
if(atWidth > 0)
doc.setTextWidth(atWidth);
if (Qt::mightBeRichText(html()))
doc.setHtml(html());
else
doc.setPlainText(html());
QSize firstSize = doc.documentLayout()->documentSize().toSize();
QSize checkSize = firstSize;
// qDebug() << "RichTextRenderer::findNaturalSize: atWidth:"<<atWidth<<", firstSize:"<<firstSize;
#define RUNAWAY_LIMIT 500
int counter = 0;
int deInc = 10;
while(checkSize.height() == firstSize.height() &&
checkSize.height() > 0 &&
counter < RUNAWAY_LIMIT)
{
int w = checkSize.width() - deInc;
doc.setTextWidth(w);
checkSize = doc.documentLayout()->documentSize().toSize();
// qDebug() << "RichTextRenderer::findNaturalSize: w:"<<w<<", checkSize:"<<checkSize<<", counter:"<<counter;
counter ++;
}
if(checkSize.width() != firstSize.width())
{
int w = checkSize.width() + deInc;
doc.setTextWidth(w);
checkSize = doc.documentLayout()->documentSize().toSize();
// qDebug() << "RichTextRenderer::findNaturalSize: Final Size: w:"<<w<<", checkSize:"<<checkSize;
return checkSize;
}
else
{
// qDebug() << "RichTextRenderer::findNaturalSize: No Change, firstSize:"<<checkSize;
return firstSize;
}
}
示例10: rowItemType
void SearchResultsItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
ResultType rType = rowItemType(index);
if (rType == ResultTypeMatch)
{
QStyleOptionViewItemV4 options = option;
initStyleOption(&options, index);
painter->save();
QTextDocument doc;
doc.setHtml(options.text);
options.text = "";
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
// shift text right to make icon visible
QSize iconSize = options.icon.actualSize(options.rect.size());
painter->translate(options.rect.left()+iconSize.width(), options.rect.top());
QRect clip(0, 0, options.rect.width()+iconSize.width(), options.rect.height());
//doc.drawContents(painter, clip);
painter->setClipRect(clip);
QAbstractTextDocumentLayout::PaintContext ctx;
// set text color for selected item
if (option.state & QStyle::State_Selected)
ctx.palette.setColor(QPalette::Text, options.palette.highlightedText().color());
ctx.clip = clip;
doc.documentLayout()->draw(painter, ctx);
painter->restore();
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
示例11: drawSimpleRichText
/*!
Draw a text document into a rectangle
\param painter Painter
\param rect Traget rectangle
\param flags Alignments/Text flags, see QPainter::drawText()
\param text Text document
*/
void QwtPainter::drawSimpleRichText( QPainter *painter, const QRectF &rect,
int flags, const QTextDocument &text )
{
QTextDocument *txt = text.clone();
painter->save();
painter->setFont( txt->defaultFont() );
qwtUnscaleFont( painter );
txt->setDefaultFont( painter->font() );
txt->setPageSize( QSizeF( rect.width(), QWIDGETSIZE_MAX ) );
QAbstractTextDocumentLayout* layout = txt->documentLayout();
const double height = layout->documentSize().height();
double y = rect.y();
if ( flags & Qt::AlignBottom )
y += ( rect.height() - height );
else if ( flags & Qt::AlignVCenter )
y += ( rect.height() - height ) / 2;
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor( QPalette::Text, painter->pen().color() );
painter->translate( rect.x(), y );
layout->draw( painter, context );
painter->restore();
delete txt;
}
示例12: ensureTextPopulated
void QLabelPrivate::ensureTextLayouted() const
{
if (!textLayoutDirty)
return;
ensureTextPopulated();
if (control) {
QTextDocument *doc = control->document();
QTextOption opt = doc->defaultTextOption();
opt.setAlignment(QFlag(this->align));
if (this->align & Qt::TextWordWrap)
opt.setWrapMode(QTextOption::WordWrap);
else
opt.setWrapMode(QTextOption::ManualWrap);
doc->setDefaultTextOption(opt);
QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
fmt.setMargin(0);
doc->rootFrame()->setFrameFormat(fmt);
doc->setTextWidth(documentRect().width());
}
textLayoutDirty = false;
}
示例13: paint
void SoftwareColumnItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
QString softwareName = index.model()->data(index.model()->index(index.row(), UpdaterDialog::Columns::SoftwareComponent)).toString();
QString websiteURL = index.model()->data(index.model()->index(index.row(), UpdaterDialog::Columns::WebsiteURL)).toString();
QString link = "<img src=\":/home.png\"><a href=\"" + websiteURL + "\">"+softwareName+"</a>";
QTextDocument document;
if (option.state & QStyle::State_MouseOver) {
// draw stuff which appears on mouse over
document.setDefaultStyleSheet("a { text-decoration: none; color: darkblue; }");
} else {
// draw stuff that appears when mouse is not over control
document.setDefaultStyleSheet("a { text-decoration: none; color: black; }");
}
document.setTextWidth(option.rect.width());
document.setHtml(link);
painter->translate(option.rect.topLeft());
document.drawContents(painter);
painter->restore();
return;
}
示例14: highlightMatches
void highlightMatches(const QString &pattern)
{
QTextEdit *ed = qobject_cast<QTextEdit *>(m_widget);
if (!ed)
return;
QTextCursor cur = ed->textCursor();
QTextEdit::ExtraSelection selection;
selection.format.setBackground(Qt::yellow);
selection.format.setForeground(Qt::black);
// Highlight matches.
QTextDocument *doc = ed->document();
QRegExp re(pattern);
cur = doc->find(re);
m_searchSelection.clear();
int a = cur.position();
while ( !cur.isNull() ) {
if ( cur.hasSelection() ) {
selection.cursor = cur;
m_searchSelection.append(selection);
} else {
cur.movePosition(QTextCursor::NextCharacter);
}
cur = doc->find(re, cur);
int b = cur.position();
if (a == b) {
cur.movePosition(QTextCursor::NextCharacter);
cur = doc->find(re, cur);
b = cur.position();
if (a == b) break;
}
a = b;
}
updateExtraSelections();
}
示例15: Q_UNUSED
SimpleResourceGraph MobiExtractor::extract(const QUrl& resUri, const QUrl& fileUrl, const QString& mimeType)
{
Q_UNUSED( mimeType );
SimpleResource fileRes(resUri);
SimpleResourceGraph graph;
Mobipocket::QFileStream stream( fileUrl.toLocalFile() );
Mobipocket::Document doc(&stream);
if( !doc.isValid() )
return graph;
QMapIterator<Mobipocket::Document::MetaKey,QString> it(doc.metadata());
while (it.hasNext()) {
it.next();
switch (it.key()) {
case Mobipocket::Document::Title:
fileRes.addProperty( NIE::title(), it.value() );
break;
case Mobipocket::Document::Author: {
SimpleResource con;
con.addType( NCO::Contact() );
con.addProperty( NCO::fullname(), it.value() );
fileRes.addProperty( NCO::creator(), con );
graph << con;
break;
}
case Mobipocket::Document::Description: {
QTextDocument document;
document.setHtml( it.value() );
QString plain = document.toPlainText();
if( !plain.isEmpty() )
fileRes.addProperty( NIE::comment(), plain );
break;
}
case Mobipocket::Document::Subject:
fileRes.addProperty( NIE::subject(), it.value() );
break;
case Mobipocket::Document::Copyright:
fileRes.addProperty( NIE::copyright(), it.value() );
break;
}
}
if (!doc.hasDRM()) {
QString html = doc.text( maxPlainTextSize() );
QTextDocument document;
document.setHtml(html);
QString plainText = document.toPlainText();
if( !plainText.isEmpty() )
fileRes.addProperty( NIE::plainTextContent(), plainText );
}
graph << fileRes;
return graph;
}