How to display plain text using QMessageBox? - qt

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());

Related

How to set font style in qml rich format Text

I'm using SF Pro Text fonts in the app. They are loaded from main.cpp:
id = QFontDatabase::addApplicationFont(":/SF-Pro-Text-Light.otf");
id = QFontDatabase::addApplicationFont(":/SF-Pro-Text-Regular.otf");
id = QFontDatabase::addApplicationFont(":/SF-Pro-Text-Medium.otf");
In main.qml I can use SF Pro Text Light font using combination of font.family and font.styleName properties:
Text {
text: "Some Text";
font.family: "SF Pro Text"
font.styleName: "Light"
}
font.styleName here can be set to Light/Medium/Regular.
But my goal is to create text line with few mixed font styles, so I tried to use the following approach:
Text {
text: "<span style=\"font-family:SF Pro Text;\">
Outer
<span style=\"font-family:SF Pro Text;\">Inner Text</span>
Text
</span>";
textFormat: Text.RichText
}
Unfortunately, I don't understand how can I pass style (medium, light or regular) inside the rich text. Could you please point me to the correct property in html or to a better approach?
Thanks.

Qt How to change selected text char by char

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);

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)

Accessing text between two QWebElement objects

I am traversing a DOM using Qt's WebKit classes. Please have a look on the following pseudo HTML:
<br>111<a class="node">AAA</a>
<br>222<a class="node">BBB</a>
...
I can easily find the anchors using findAll(). However I also need to get the text before the elements ("111" and "222"). I tried to use previousSibling() but of course that gives me the <br> element since the "111" and "222" texts are no elements.
I found a function to access text within an element, but how can I access between the <br> and the <a> elements?
It seems it is not possible. The only workaround I could find is getting the plain text of the parent node and parsing the resulting plain text.
This is the way I solved it:
QWebElement *element = ...
// find out if QWebElement has text
QDomDocument doc;
doc.setContent(element->toOuterXml());
QDomElement domelem = doc.documentElement();
for(QDomNode n = domelem.firstChild(); !n.isNull(); n = n.nextSibling())
{
QDomText t = n.toText();
if (!t.isNull())
{
// it has text !
qDebug() << t.data();
break;
}
}

How to get title from HTTP text representation via Qt?

Qt 4.7 DOM API seems kind of strange :(. I have a text HTML representation and need to get "title" text. Seems very easy, but following code is not working, i get empty string:
QDomDocument dom;
dom.setContent( "<html><head><title>this is a title</title></head></html>" );
QString title = dom.elementsByTagName( "title" ).item( 0 ).nodeValue();
Any suggestions?
Try this:
QString title = dom.elementsByTagName( "title" ).item( 0 ).firstChild().nodeValue();
Because the tree structure for text is:
node <title>
=> text element

Resources