How to vertically align Qlistwidgetitem text with the icon? - qt

I'm adding icons and text as Qlistwidgetitem in Qlistwidget.
icon size is 24*24, and it is on the left of the text. After adding the icon, text appears to be on the top of the available space, I want to align it vertically in the available space.
I tried itemToAdd->setTextAlignment( Qt::AlignVCenter) but it is not showing any effect.
Could you suggest any other method to do it?
See text are at the top of available space.
myQlistWidget->addItem(new QListWidgetItem( *GuiIconManager::ref().find("icon") , nodeValue()) );
*GuiIconManager::ref().find("icon") returns QIcon size of 24*24 and nodeValue() returns QString.
OS is linux.

Make sure the string you're passing into QListWidgetItem doesn't have a newline at the end of it

Related

How to align a label in Qt Designer without cutting the label content?

I'm trying to create a widget that will act as a navigation bar. It is formed by some buttons that are separated by arrows.
I'm trying to replicate this picture in Qt Designer using labels with the arrow character → . The problem is that this character is bigger than it seems so when I resize the container, the lower part of the arrow disappears.
I have tried to use the vertical layout using a vertical spacer to align the arrow character as the same height as the buttons but the character was cut anyways.
I can't reproduce your example by using just the standard settings.
I added two buttons and a label to the horizontalLayout and set the label font size to 55. I also set the text alignment to horizontal center and vertical center.
You will always have more space at the top cause this is the content of the ascii char.
Maybe it's better to use a simple icon?
EDIT:
You can also add a label or a button with a pixmap/icon which is resizeing with the window:

Vertical layout of the icon and text in a JMenu

I try to find a way to place the icon of a JMenu above its text. I've seen the method setComponentOrientation(..), but it influences only the horizontal order of the icon and the text (icon on the left side, text on the right - or the opposite). Is there any possibility to place these parts of a JMenu vertically (icon on the top, text under the icon)?
I'm not sure if you are fully able to do that. Maybe this page can help you out. http://www.java-forums.org/awt-swing/36112-adding-jpanel-jmenu-focus-issues.html I have never really tried adding an image onto a JMenu vertically. You could always try using a JPanel, and adding the JMenu into it, and then setting the layout of the JPanel as a BoxLayout
JLabel imageForIcon = new JLabel(// image path );
JPanel p = new JPanel(imageForIcon);
JMenu m = new JMenu(p);
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
I am not sure if this would work, but it seems as it could possibly be an option to consider.

QTextEdit::adjustSize() not working?

Setting text for QTextEdit:
te->setPlainText(“Something”) ;
te->adjustSize();
should wrap around “Something” only, instead the QTextEdit is expanding to its maximum Width-Height, can’t fix it..
When I select “Something” on run time, only “Something” is highlighted, no added extra white spaces.
Expectations: when text is small enough to fit on one Line, the text edit shouldn’t expand in height, when the text needs to wrap, only the extra line width should be added not the maximum width.
if adjustSize(); is not called, the text will wrap on the width that was set in the .ui in the Creator, won't dynamically expand horizontally nor vertically..
Some Info:
Horizontal Policy: ExpandingVertical Policy : MinimumExpanding
minimumSize : 2×22maximum Size : 300×100lineWrapMode:
WidgetWidth
Yes, looks like there is no easy way to count lines in QTextEdit.
adjustSize() is made for QWidget and is not reimplemented for QTextEdit, it is based on sizeHint().
You can use your own method to count lines, f.e.
You can use QFontMetrics to calculate width of every word in your text
You can set height to 22 and increment it until maximumHeight hitted or vertical scrollbar dissapears.
You can get some info from sources of QTextEdit itself and subclass it, reimplementing something (adjustSize()?) there.

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:

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)

Resources