views:

132

answers:

1

I want to format a selected text in a QTextEdit by clicking a button. For axample I want to make it bold if it is not-bold, or not-bold if it is bold. Please help me with an example.

EDIT:

Actually I have found already a code - qt demo for text editor which does what I need:

void
MyTextEdit::boldText(bool isBold) //this is the SLOT for the button trigger(bool)
{
    QTextCharFormat fmt;
    fmt.setFontWeight(isBold ? QFont::Bold : QFont::Normal);
    mergeFormatOnWordOrSelection(fmt);
}

void
MyTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
 {
     QTextCursor cursor = m_textEdit->textCursor();
     if (!cursor.hasSelection())
         cursor.select(QTextCursor::WordUnderCursor);
     cursor.mergeCharFormat(format);
     m_textEdit->mergeCurrentCharFormat(format);
 }

But I can't understand what returnes the textCursor() method, and how the merging of properties is being done? Just some formats are being changed, some of them stay constant. How mergeCharFormat function understands what to change and what to leave as is. Please explain me just these 2 things. Thanks.

+3  A: 

The textCursor() returns a textCursor that contains the position of the cursor you use in the textEdit, see QTextCursor in Qt classes. So by selecting the text that is contained by the cursor start and end position, you have the text that is currently highlited.

As for the mergeCharFormat, I guess that it is used to apply a new state (bold, italic, underlined) and to keep the existing ones. Say your text is already underlined and you apply bold, you would want to keep both.

Hope this helps.

Live
And where is the button? Or where is the selected text?
Narek
You can connect the triggered signal of a button to a slot that places the HTML tags at the begin and the end of the selected text.To find what is the text currently selected, you would have to check that with QTextEdit::textCursor() that returns a QTextCursor. I will try and update the example above.
Live
You are right, but if the text is italic and I make it bold with boldText(), then seams that text should become bold ONLY (and lose italic-ness) as I create a new QTextCharFormat object the italic parameter of which by default is non-italic (that is to say false). So the cursor.mergeCharFormat() function should consider italic parameter changed as well and thext should lose its italic-ness. Are you agree?
Narek
I think that if the text selected was italic to begin with, it would remain italic unless you change the italic property. That is because you merge the new format with the format of the currentChar.
Live
No, I guess you did not get me. Indeed what I don't understand is not the definition of merging, but implementation. How can the function know what is set newly and what is just set by default constructor. The only thing I may think, that while implementing the constructor of QTextCharFormat the developer says that all parameters are not assigned (not configured). And the params get value if you explicitly assign it something. Otherwise the parameter is not configured, and if it is not configured, then some default value is being used for that parameter if it is necessary.
Narek
The better way to see it is that when you merge, the format stays the same for every aspect of the format, _except_ the ones you specified in the QTextCharFormat you just created. So for the implementation, I guess you are right, the constructor does not give a true of false value, but an uninitialized value to all the properties, and they stay that way until you specify a change, in our case the bold.
Live
Ok thanks. I needed a discussion to be sure. Thanks for your great help and +1 to your answer and to your all comment as you tried hard to help me! :)
Narek