QTextEdit change the font of a single paragraph / block

Using QTextEdit, I need to change the font attributes of each paragraph individually. This is similar to how many word processors change the font of the paragraph when the user selects a style from the menu (unspecified formatting).

Ideally, I would like to apply a QTextCharFormat (or equivalent) to the block (paragraph) immediately before it is displayed and displayed , but I would prefer that the font attribute is not inserted in the text, since I do not want this information in the file, but I need save any bold / italic / underlined attributes that the user could set for words in paragraphs (I intend to save the necessary information in QTextBlock :: userData). However, I cannot figure out where I will need to insert a function to complete this task.

I decided that I can’t change the QTextCharFormat of the paragraph from QTextBlock or QTextCursor, since this only applies to new blocks, it does not affect blocks with existing text.

I checked QTextLayout, but I don't think my answer is.

I have been looking for a solution to this problem for several days. I would be very kind to any pointer in the right direction.

I have many years of experience with C ++, but I'm a little new to Qt. Using Qt 4.8.

Edit:

() , . , , , - (, ) . ( ) , , , . Qt , , , . , . , , .

+4
3

[ ]

#include <QTextEdit>    // not needed if using the designer

#include <QTextDocument>
#include <QTextBlock>
#include <QTextCursor>

[]

QTextDocument

, . QTextDocument::findBlockByNumber , , , .

QTextBlock

. .

QTextCursor

, QTextBlock . QTextCursor , .

[ ]

// For block management
QTextDocument *doc = new QTextDocument(this);
ui->textEdit->setDocument(doc);  // from QTextEdit created by the Designer
//-------------------------------------------------
// Locate the 1st block
QTextBlock block = doc->findBlockByNumber(0);

// Initiate a copy of cursor on the block
// Notice: it won't change any cursor behavior of the text editor, since it 
//         just another copy of cursor, and it "invisible" from the editor.
QTextCursor cursor(block);

// Set background color
QTextBlockFormat blockFormat = cursor.blockFormat();
blockFormat.setBackground(QColor(Qt::yellow));
cursor.setBlockFormat(blockFormat);

// Set font
for (QTextBlock::iterator it = cursor.block().begin(); !(it.atEnd()); ++it)
{
    QTextCharFormat charFormat = it.fragment().charFormat();
    charFormat.setFont(QFont("Times", 15, QFont::Bold));

    QTextCursor tempCursor = cursor;
    tempCursor.setPosition(it.fragment().position());
    tempCursor.setPosition(it.fragment().position() + it.fragment().length(), QTextCursor::KeepAnchor);
    tempCursor.setCharFormat(charFormat);
}

: QTextEdit ?


[]

: Qt 4.8 + MSVC2010 + Windows 7 32

, .

enter image description here

1 ( , )

enter image description here

2

enter image description here

+7

QTextCursor .

. , .

.

0

QTextEdit HTML, , , - HTML. . :

QString text = "<p><b>Paragraph 1</b></p><p><i>Paragraph 2</i></p>";
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertHtml(text);

- :

1

2

, HTML, Qt. . HTML

0

Source: https://habr.com/ru/post/1569508/


All Articles