Enable QLabel to shrink even if it truncates text - qt

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().
But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.
Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)

Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

Related

Having trouble resizing a QLabel in a QScrollArea

I'm trying to follow the example at the below link to have a picture (in a qlabel) shown in a scrollable area.
https://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html
I'm using Qt Designer to make the ui instead of hardcoding everything. So I have a QLabel, in a QWidget (with a grid layout assigned to it), in a QScrollArea.
From the tutorial, they state the following for the sizepolicy of the QLabel:
We set imageLabel's [QLabel] size policy to ignored, making the users able to scale the image to whatever size they want when the Fit to Window option is turned on. Otherwise, the default size policy (preferred) will make scroll bars appear when the scroll area becomes smaller than the label's minimum size hint.
Setting it to ignored fits to the window, as expected and as stated. Setting it to preferred provides scroll bars when the image is larger than the scroll area, also as expected and as stated. My issue is that when the sizepolicy is set to preferred, the resize function of the QLabel doesn't work. It always stays at the default size of the loaded image. The only way that I'm able to get the resize function to work is when I don't assign a layout/break the layout to the widget in the QScrollArea, but then no scrollbars will appear when the image is larger than the QScrollArea.
Does anyone have any ideas of how to make the resize function and scrollbars work at the same time?
Thanks in advance for any help. I'm trying to learn qt5 still and this seems like it'd be a simple thing to do, but it's slowly driving me crazy.

Displaying an image and automatically re-size it

I can't quite figure out what the best way of displaying an image is in my particular case, so hopefully someone on here has a few tips.
I want to display an image that gets re-sized automatically to fit inside the space that is available. I currently do this by creating a class derived from QLabel that implements void resizeEvent(QResizeEvent*) where I do a QPixmap::scaled to re-size the image. The problem is that this only works when the widget is enlarged because the widget doesn't get a resizeEvent when I try to make the widget smaller. I guess that because I set the image to the same size as the widget, it isn't allowed to be sized smaller again? I guess I could try to create a smaller image therefor introducing a sort of "border" around the image which would perhaps allow re-size events to occur when making the area smaller. Any thoughts?
resizeEvent is sent whenever size is changed. It doesn't matter whether it is enlarged or not.
But you can set Policy and Max/Min size to constraint widget in shrinking/enlarging. So if you have your widget not getting resizeEvent AND it doesn't shrink either, then look at your size policy and min width/height. If it shrinks but you doesn't have resizeEvent then you have some error in you logic, I believe.
Alternatively you can use paintEvent for image painting and use QWidget::rect() for your widget width/height.
Try changing the size policy of the label to QSizePolicy::Preferred.
Have a look at size policies in general.

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.

How to make a Qt widget change its size?

My problem probably is very simple, but I have no idea how to solve it.
So, I have several widgets in vertical layout. Some of them in some moment should be invisible. I suppose this moment the control that has expanding vertical policy should increase its height but it never happens. Why? How to force it to change the size? So far there is only one way to do that - to change the size of window manually a little bit and only after it the widget changes its height.
When you make a widget invisible (i.e. call hide), also remove it from the layout (see removeWidget). That way all of the other widgets will automatically resize to make use of the extra space. Since you are only hiding the widget, its space is still reserved in the vertical layout.
Make use of insertWidget to maintain the layout ordering when transitioning a hidden widget back into view. Otherwise, addWidget would always place it at the bottom. This approach should be less complex than managing the sizes manually.
Try calling adjustSize() on the parent of the layout.
Have you tried setting the stretch on 1 for the widget you want to be expanded when others are hidden?
QBoxLayout::addWidget( my_widget, 1 );
When this widget is added with a stretch of 1 and the other widgets without stretch, this widget will expand to the available space. Maybe that will do the trick.

qt unexpandable layout?

Ok, here is my problem:
I have a vertical layout which contains a QPlainTextEdit and a horizontal layout (containing 2 QPushButtons) below the text edit.
The vertical layout is just a part of GUI, and gets resized depending on screen resolution. Btw. it is a mobile app, so I don't have a lot of space on screen.
Push buttons have some text which is dynamically set, I don't know it from the beginning to code it manually.
My problem occurs when the text in push buttons is big, and my whole vertical layout is expanded to fit the buttons.
How can I make the vertical layout unexpandable? note, that this is different from "fixed" because of different screen resoulutions.
I'd just like the clip the buttons if they do not fit, but keep the layout width untouched.
Anyway to do this?
You'll need to set the maximum width for the buttons, not the layout, which is only widening to fit the wider buttons. Check out the docs on QPushButton and look for QWidget inherited functions called setMaximumSize or setMaximumWidth.
You can always GetWidth() on the button when it is an appropriate size, then setMaximumWidth using that value since you wouldn't ordinarily know this. Pick an appropriate default text size/val and use that to create your "dynamic" default since this is going on screens of varying size.

Resources