PDA

View Full Version : QTextEdit. To make selected text as Bold.



KIBSOFT
7th January 2010, 00:12
There is a QTextEdit, and how to make selected text as Bold?

P.S. Sorry for probable mistakes in the text.

nikhilqt
7th January 2010, 09:56
use html tags while setting a text.

textEdit->setText("<html><b>Hello</b</html>");

go through QtAssistant, checkout "QTextEdit Class".

KIBSOFT
7th January 2010, 10:44
I use textCursor() method for return selected text and then I don't know what to do with it :(
In documentation I found QTextFormat Class, but i don't understand how to use it..

KIBSOFT
7th January 2010, 13:55
I found an example in the documentation:

QTextDocument *document = edit->document();
QTextCursor cursor(document);

cursor.movePosition(QTextCursor::Start);
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);

QTextCharFormat format;
format.setFontWeight(QFont::Bold);

cursor.mergeCharFormat(format);//do the text as Bold

nikhilqt
7th January 2010, 15:32
Ok. I just tried a way. But you need to do tweak in a little bit for total functionality.

connect the signal slot,



connect(ui.textEdit, SIGNAL(copyAvailable(bool)), this, SLOT(formatText(bool)));

and slot goes like this



void samplewidget::formatText(bool bSelectState)
{
if(!bSelectState)
return;

ui.textEdit->copy();

QClipboard* pClip = QApplication::clipboard();
QString str = pClip->text(QClipboard::Clipboard);

QString sFullString = ui.textEdit->toPlainText();
int pos = ui.textEdit->textCursor().position();

pos -= str.length();
QString str1 = "<html><b>" + str + "</b></html>";
sFullString.replace(pos, str.length(), str1);

ui.textEdit->setText(sFullString);
}

Chumpen
14th December 2016, 11:47
This is the code that is easy for me.


QTextCharFormat format;
format.setFontWeight(QFont::Bold);
ui->plainTextEdit->textCursor().mergeCharFormat(format);