Change checkbox position for a QCheckBox - qt

I have a QCheckbox in a grid layout defined as such:
self.isSubfactorCheckbox = QtGui.QCheckBox('Is &subfactor', self)
By default the checkbox is to the left of the "Is subfactor" text. I wish to move it to the right of the text.
I've tried playing around with subcontrol-position but to no avail.
self.isSubfactorCheckbox.setStyleSheet('QCheckBox::indicator{subcontrol-origin: content; subcontrol-position: top right;}')
This moves the checkbox to the right (it's still on the left of the text) but it pushes the text to the right and out of the window's edge.
This didn't help to move the text to the right:
self.isSubfactorCheckbox.setStyleSheet('QCheckBox::text{subcontrol-origin: content; subcontrol-position: top left; }')
A solution would be creating a QLabel and adding the checkbox on its right but I don't haven't found a way to underline letters in a QLabel so that the user will know which is the shortcut key. I've tried prefixing letters with & or wrapping them in <u> </u> tags.
I'd still prefer to use only a QLabel, but how would I switch the checkbox to the right of the text without the text getting pushed out.

Even easier...
Use the QWidget.setLayoutDirection method and set it RightToLeft vs. LeftToRight
You can do this in Designer or through code, it is a property or all widgets.

Use the buddy mechanism. Set the QCheckBox to the buddy of the QLabel. After that prefix & will underline the shortcut character in the label. See the documentation of the QLabel's setBuddy.

Related

QT combobox's item border is see throgh QAbstractItemView's radius

I would like to have a rounded rectangle shape of the popup by combobox, which is easy to implement as I add a "border-radius: 20px" to QAbastractItemView.( the 20px is just for easy identification for demo)
However, I also need to have a separator line between combobox items. I use item's bottom-border style to let items look like they have separator. But as you could see in the snapshot, there will be a extra bottom border appear behind QAbastractItemView.
How can I hide this extra bottom-border ? I search Qt's documentation, QSS has no :last-child or nth-child equivalent as in CSS. And there is no sub-control like ::separator, so I cannot sytle the separator.
Could anyone has some clue ? Thank you.

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.

Qlabel with image as the background overlaps other Qlabels

I have a Qlabel with image as rich text and some other Qlabels on top of that as in the picture:
although I sent the Qlabel with image to back but when I run they appear as follows:
is there anyway to fix this?
Make the text labels children of the label containing the fade. Also I can not see any layouts. Did you use layouts? You could also put the fade on the widget by implementing its paintEvent(). All other widgets will be displayed on top of that.
Try right clicking the image label and clicking the send-to-back option. That might work. That should send the QLabel behind the other elements even though they appear as though they are already in front.

QTextEdit: scroll down automatically only if the scrollbar is at the bottom

There's a QTextEdit that displays quite a lot of text. It is NOT editable. Suppose I want to read something around the beginning, scroll up, but then a new line is added and the scrollbar automatically goes to the bottom. I experience similar problems when using various programs (regardless of the language they were written in). How does one deal with this problem?
The behavior I want when a new line is added to the text:
if the scrollbar is at the bottom, scroll down automatically.
if the scrollbar is elsewhere, don't scroll
I suppose that
ensureCursorVisible()
is not the solution, since the QTextEdit is not editable, the user won't click inside it, and the position of the cursor is not the same as the position of the vertical scrollbar.
I would make Scroll bar position listener, which will remember position on scrolling (and also check is it at the bottom or not).
Then, when new line is added, check is it at bottom, if is scroll down, if is somewhere else then scroll back to that position.
Check this QScrollBar, you can grab it from QTextEdit via horizontalScrollBar() and verticalScrollBar().
More concrete, I would connect slot with signal from QScrollBar - valueChanged(int value) and play with values as it is described here.
It is not necessary to connect a scrollbar listener. Just query the scrollbar before appending text:
QScrollBar *scrollbar = textedit->verticalScrollBar();
bool scrollbarAtBottom = (scrollbar->value() >= (scrollbar->maximum() - 4));
int scrollbarPrevValue = scrollbar->value();
The "minus 4" hack in scrollbarAtBottom is necessary since ensureCursorVisible() does not scroll exactly to the bottom, but some fixed amount above. Check it with your font sizes.
Now you can insert the text:
textedit->moveCursor(QTextCursor::End);
// begin with newline if text is not empty
if (! textedit->document()->isEmpty())
textedit->insertHtml(QStringLiteral("<br>"));
textedit->insertHtml(QStringLiteral("My text here."))
After that operation, either scroll to the bottom, or fix the scrollbar such that it does not move at all:
if (scrollbarAtBottom)
textedit->ensureCursorVisible();
else
textedit->verticalScrollBar()->setValue(scrollbarPrevValue);

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:

Resources