Qt How to change selected text char by char - qt

I'm creating a simple Text Editor in Qt.
I'm able to edit selected text and make it, for example, bold or underline or both. The problem is when the selected text is partially bold, normal or other.
So the only way to make it good is to take the selected text and edit it char by char (if it is already cursive and i want it bold too, the char must be both).
This is part of my code in which i can change selected text into bold:
QFont font;
QTextCursor cursor = ui->textEdit->textCursor();
QTextCharFormat format;
if(cursor.hasSelection()){
font = cursor.charFormat().font();
if(!cursor.charFormat().font().bold()){
font.setBold(true);
format.setFont(font);
cursor.setCharFormat(format);
}
else{
font.setBold(false);
format.setFont(font);
cursor.setCharFormat(format);
}
ui->textEdit->setTextCursor(cursor);
The cursive function is identical.
This will not work with text already edit.
Example:
randomtext
Now i want to select part of the text such as: "ndomte" and make it all bold. My result is:
randomtext
What i want is:
randomtext
How can i do it?

Perhaps you can use QTextCursor::mergeCharFormat(const QTextCharFormat & modifier)? http://doc.qt.io/qt-5/qtextcursor.html#mergeCharFormat
Example:
QTextCharFormat format;
format.setFontWeight(QFont::Bold);
cursor.mergeCharFormat(format);

Related

How to display plain text using QMessageBox?

I need to display an HTML text:
QString text="<b>Hello</b>";
QMessageBox::information(this,"info", text);
The text is displayed as bold "Hello". How to display it as it is, i.e.,
<b>Hello</b>
Thanks!
You have to use the method toHtmlEscaped() of QString:
QString text="<b>Hello</b>";
QMessageBox::information(this, "info", text.toHtmlEscaped());

how to display part of text in qcombobox (QT)

My understanding is the current display text is related to the qcombobox itemlist content.If one item length is very long and wider than qcombobox's width.how to just display part of the text on the qcombobox?
seen from the above picture, QT already displays part of the text(the full item is ending with 'ker', the display item is ending with 'sdl') but can I control the exact display text? because my combobox has a background image and the arrow will be more left than the that in the above picture .
Another question is can I control the item text display in dropdown window as well? QT replaces some words with '...' but I like to control it myself.
try this
m_combobox->addItem("a very long long long long long long text ");
QFont font("times", 24);
QFontMetrics fm(font);
QString elidedText= fm.elidedText("a very long long long long long long text ",Qt::ElideRight, 80);
m_combobox->addItem(elidedText);
you can make a function that takes a QString as parameter and return a QString you give it your text and it return the elided text.
you can store an arbitrary substring as items text and the whole string in userData:
in pyqt:
for text in itemlist:
combobox.addItem(text[0:n], text)
and get the displaytext, userData by:
combobox.currentText()
combobox.currentData(QtCore.Qt.UserRole)
or by
combobox.itemText(index)
combobox.itemData(index, QtCore.Qt.UserRole)

how to change the text added to the scene?

I want to add the text in my scene.and this text sometimes should be changed.how to change the text added to scene ? suppose i have a player that earned points and i put his points in my scene.when his points increase or decrease i have to change the text.how to do this work?
QString points;
QGraphicsTextItem* text;
QFont font;
font.setBold(true);
font.setPointSize(50);
font.setItalic(true);
te="Ponit";
text=scene->addText(points,font);
text->setPos(100,100);
text->setDefaultTextColor(QColor("red"));
When you call QGraphicsScene::addText, it returns the QGraphicsTextItem. Using this object, you can set either plain or HTML text. For example: -
text->setPlainText("Some New Text");

QT - Increase Richtext size on Button click

How do i increase the size of a Rich Text on the click of a button ?
I have a QTextEdit box with Rich text pasted in it.On the click of a + [ui button] i need to increase the font size of all the text inside it. Any idea on how to do that ?
Solution
This is what you should do inside the slot :
//-------------------------desired format-------------------------------
qreal pointSize = 40; // 40 for example, you can parameterize it
QTextCharFormat format;
format.setFontPointSize(pointSize);
//----------------------------------------------------------------------
ui->textEdit->selectAll();
// ^^^^^^^^^^^ You ask for all text in the textedit
// But remember partially change with mouse selection is also doable
ui->textEdit->mergeCurrentCharFormat(format);
(P.S. ui->textEdit is a pointer to QTextEdit)
The key point is to create an instance of QTextCharFormat to set the "partial" information of the font (Ex: size information only) and use QTextEdit::mergeCurrentCharFormat to merge the original format with the new format.
For example:
After merging by the operations above, the color, font...etc except size will be retained:
You can use the QTestEdit::setCurrentFont() function. For example:
QTextEdit te;
QFont f = te.currentFont();
int oldPointSize = f.pointSize();
int newPointSize = oldPointSize + 10;
f.setPointSize(newPointSize);
te.setCurrentFont(f);
te.setText("Test");
te.show();

Qt: change color part of text

This is my code:
QTextCursor cursor = ui->editor->textCursor(); // editor is QTextEdit
cursor.select(QTextCursor::WordUnderCursor);
QString c = cursor.selectedText();
if (c == keywords[i])
{
cursor.removeSelectedText();
cursor.insertHtml("<font color=\"DeepPink\">" + keywords[i] + "</font>");
}
So, if the keyword is "new", this word is colored pink. The problem is that everything that is inserted after "new" is also colored red, and not the standard black.
Anybody? :)
UPDATE:
Stupid me. Just added ui->editor->setTextColor("#000000");
ui->editor->setTextColor("#000000");

Resources