how to change the text added to the scene? - qt

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

Related

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

QLabel setText not displaying text of QLineEdit if label size small

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.

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)

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

increase selection outline width in QGraphicsTextitem

i have a QGraphicsTextitem with text interaction where user can edit the current text and add the new text . but my new requirement is to increase the selection outline width and can be controlled by a QSlider. is it possible to increase the dotted selection width of the QGraphicsTextItem.
i wanted to increase the pen thickness or size of the selection box coming around the text ..
in the image a dotted lines bound the text . is it possible to increase the dotted line pen size or thickness.
This question is ancient, but I'll do my civic duty to try an answer:
There are a few things you must do.
Subclass your QGraphicsTextItem
Override the paint method, and inside it remove the default selection style, and draw your own
void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
QStyleOptionGraphicsItem opt(*option);
// Remove the selection style state, to prevent the dotted line from being drawn.
opt.state = QStyle::State_None;
// Draw your fill on the item rectangle (or as big as you require) before drawing the text
// This is where you can use your calculated values (as member variables) from what you did with the slider
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::green);
painter->drawRect(whateverRectangle());
// Call the parent to do the actual text drawing
QGraphicsTextItem::paint(painter, &opt, widget);
// You can use these to decide when you draw
bool textEditingMode = (textInteractionFlags() & Qt::TextEditorInteraction);
bool isSelected = (option->state & QStyle::State_Selected);
// Draw your rectangle - can be different in selected mode or editing mode if you wish
if (option->state & (QStyle::State_Selected))
{
// You can change pen thickness for the selection outline if you like
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DotLine));
painter->setBrush(Qt::magenta);
painter->drawRect(whateverRectangle());
}
}
You may have to override the boundingRect, opaqueArea and shape functions to account for your increased size.

Resources