Qt - How to change the direction of placeholder in a QLineEdit? - qt

I am using Qt 4.7.4 and my application language in RTL.
I have set the application layout to RightToLeft.
So everything is now right to left except placeholders in QLineEdit.
I tried setAlignment method
but it changes the text and placeholder direction in reverse.
Then I tried setStylesheet but it does not work for
direction:rtl;
text-align:right;
unicode-bidi:embed;
I think it is a bug, Is there any fix?

The changelog for version 4.7 reads as follows:
QWidget::setLayoutDirection no longer affects the text layout
direction (Qt::LeftToRight or Qt::RightToLeft) of QTextEdit, QLineEdit
and widgets based on them.
The default text layout direction
(Qt::LayoutDirectionAuto) is now detected from keyboard layout and
language of the text (conforms to Unicode standards).
To
programmatically force the text direction of a QTextEdit, you can
change the defaultTextOption of the QTextDocument associated with that
widget with a new QTextOption of different textDirection property.
For QLineEdit, the only way so far is sending a Qt::Key_Direction_L/R
keyboard event to that widget.
Hope that helps.

Related

QToolButton Space Between Text and Icon

I have a QToolButton. I wanted text and icon to be on it. I set button style by setToolButtonStyle(Qt::ToolButtonTextBesideIcon).
But icon and text are so close to each other. Is there a method to give some space between icon and text by css?
You can't. There is no such a property neither for QToolButton (nor for QPushButton). All the properties for these buttons are in the documentation.
One thing you can do is to create your own class inheriting from QToolButton and overriding the paintEvent(). In this function, you will manually place your icon.
This is the shortest solution, but if you're brave enough, there are longer paths, like creating your own button subclassing directly QWidget (but in this case, you will need to implement ALL its behavior).
You can achieve it although this is not the documented solution.
Just provide spaces in QPushButton text start. By Doing this, icon and text will automatically go far from each other.

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.

Qt, QWidget, QTabWidget, QTabBar: How to move the text to the bottom of icon?

By default, the text is always on the right-side of the tab icon...
What should I do to move the text to the bottom of it ?
And is there an option to do icon/text alignment ??
(I'm designing the GUI base on Qt Creator Designer.
I'm trying with Qt stylesheet but I can't. I have not yet modify the code that generated by Qt Designer.)
Thank you very much!
A tricky way:
Since we can set QToolButton to be icon above text, just create a group of QToolButtons, listed horizontally or vertically, each button needs to be checkable. Add them to a QButtonGroup.
Then hide the TabBar of QTabWidget(out of parent widget or under ToolButtons), place the TabWidget under the listed ToolButtons.
Finally, connect the QButtonGroup buttonClicked signal to the TabWidget's setCurrentIndex signal.
Notice, when you add a button to QButtonGroup, you will have to assign the ID from 0 manually.

How to set QLineEdit default text to one space?

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)

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