How to set QLineEdit default text to one space? - qt

I entered one space as a QLineEdit's text in the Qt Creator GUI Designer. I can see that the space is there in the designer wiew, but if I compile and run it, the space disapears. I want this space to be a QLineEdit's default text, how can set it, or tell Qt to do not delete that one space?

My guess is that space disappears because UIs are stored as XML and nodes consisting only of whitespace are strippd (see this question).
But you can set the space to the QLineEdit in the window's constructor:
ui->lineEdit->setText(" ");

If what you want is to have a default text when the widget is empty, use the setPlaceholderText(QString)
If you want to just set a initial value, then do it in the constructor of your app/widget/class with setText(QString)

Related

Show the log text in QMainWindow with MdiArea

I have developed an application in Qt with QMainWindow as a main Widget, and added Mdiarea I needed for adding QMdieSubWindows.
I want know how to have a logging area like in Qt Creator.
My log text is basically what is going on. As
Started the optimizer ...
File is saved ...
The file is not loaded ...
and etc.
I thought of adding a QPlainTextEdit or a QTextEdit, and just append text to them.
I wrote this in my QMainWindow.cpp:
QPlainText* mydebugger = new QPlainText(this);
mydebugger.appendPlaintext("Debugger started");
mydebugger.show();
But this is showing the plainText over my Menu in QMainWindow;
I would like to have it on the bottom, above my StatusBar.
I would like to ask now:
QPlainTextEdit or QTextEdit: which one is better for my task? I need only appending the text, and maybe highlighting, and coloring the text.
How to get the Q(Plain)TextEdit as for example in QtCreator at the bottom with fixed position and fixed width?
I tried to create an MdiSubWindow and add the plaintext widget into it, and show it.
It works as I wanted, and I can add text in it. But I can not still make fixed at the bottom. Any ideas?
If you want colour and other formatting options, QTextEdit is your way to go. QPlainTextEdit does not allow formatting.
You're better off using a QDockWidget than a QMdiSubWindow. You can then dock your logger at the bottom of your main window.

Fitting the label text of tab widet Qt

I have a tab widget with two tabs in it in my Qt application. When I set the label text for a tab to a long word like 'Difference', some of the text goes out of the boundary of the tab and is not shown. How do I fix this so that the entire label text fits exactly in the tab?
Thanks,
Rakesh.
Qt receives that style from OS. By default on Windows text isn't truncated. You can set it manually using
QTabWidget::setElideMode( Qt::ElideNone );
I think elideMode would be helpful for you.
Try setting the value to Qt::ElideNone which will not show the ellipsis in the text, so I am guessing it might show the entire text.

QLabel auto multiple lines

For example, we have a QLabel with MaximumWidth set to 400.
When we try to display some text with pixel width more than 400, it's shown cut off.
Is there any way to make QLabel display this string in multiple lines without using QFontMetrics or the like?
If I understood your question correctly, you should use the setWordWrap function for your label, with true as its parameter.
QLabel lbl("long long string");
lbl.setWordWrap(true);
In order to show multiple lines in QLabel, right click on QLabel and select 'change rich text'. This brings up dialog where you can type the text as you want to see including enter key. Setting the word wrap is not required for this.
If you set the word wrap as well (in QLabel properties) than it will wrap each individual line in the Qlabel if it was longer than the real estate.
As another option to wrap text using Qt Designer, you can check the box under Property Editor for a QLabel:

what property should i set to Fit the text in label

I want to display a big string in a Qlablel for this I simply have created a label in the Qt GUI editor.
Then set the string with the Wordwrap property to "ON".
Here text is not coming to the next line itself. Instead, it's crossing the view region.
However, if I give an "\n" it works well.
How do I put up the big string in a label to display in a visible region?
label->setWordWrap(true);
See the QLabel::setWordWrap(bool on) documentation.

QLabel: interlinea/linespacing in WordWrap mode

How do I set line height in QLabel when in WordWrap mode?
Use HTML text:
QString template = "<p style=\"line-height:%1%\">%2<p>";
QString targetText = template.arg(myPercentage).arg(myTex);
QLabel *l = new QLabel(targetText, this);
where myPercentage is like 60 - 80.
You will get condensed lines in the WordWrap mode
There is no line spacing property in QLabel. You can change the widget font, which will change the line's height, but I suspect that is not what you want.
Line height is computed from the QFont of the widget and can be obtained by the QFontMetrics associated with the widget. Using this information, you may create your own widget that has a line spacing property (and a text wrap mode), but that represents a lot of low-level work.
You can also edit the HTML of the QLabel directly in Qt Designer.
Select the label in Qt Designer.
In the Property Editor, under the QLabel section, select the text property and press the ... button.
Select the "source" tab and edit the HTML from there.
Here are two examples that control the line spacing of a QLabel using HTML (tested in Qt 5.7). I am sure there are many more (and some better) ways to write the HTML, but this should be a good start.
Example 1
<html><head/><body>
<p style="line-height:120"><span>
This is the first line of the label.<br>
This is the second line.<br>
This is the third and final line.
</span></p>
</body></html>
This example is neater if the line spacing is the same for the whole paragraph.
Example 2
<html><head/><body>
<p style="line-height:20"><span>This is the first line of the label.</span></p>
<p style="line-height:20"><span>This is the second line.</span></p>
<p style="line-height:100"><span>This is the third and final line.</span></p>
</body></html>
This example allows you to control the spacing of each line individually. I had to make the height of the last line 100 to prevent Qt from cutting it in half. I assume it affects how Qt calculates the height of the label as a widget.

Resources