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)
Related
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());
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);
I have a QLabel in which I want to show text from a QLineEdit. The size of line-edit is bigger than the label so I want to show the label ending with a dotted line
ui->LE_Serverpath// contains 20 charecters
ui->LB_UsernameInfo // having size of 10 charecters
ui->LB_UsernameInfo->setText(ui->LE_Serverpath->text());
using wordwrap the line is getting cut but I need dotted lines at the end
ui->LB_UsernameInfo->setWordWrap(true);
You have to set an ElideMode for the label, you need a QFontMetrics instance from label font then set text elide mode for the label. to show dots by end of line, set ElidMode to Qt::ElideRight, on the text copied form QLieEdit:
//QFontMetrics metrics(ui->LB_UsernameInfo->font()); // QLabel already has font metrics
int width = ui->LB_UsernameInfo->width() - 2;
QString text = ui->LB_UsernameInfo->fontMetrics().elidedText(ui->LE_Serverpath->text(), Qt::ElideRight, width);
ui->LB_UsernameInfo->setText(text);
ui->LB_UsernameInfo->setWordWrap(true);
use Qt's layout classes, it will take care of the resizing according to text width.
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");
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();