1
votes

Goal:
I'm trying to display several instances of a custom widget in a list. The widget might contain long user-defined paths, therefore I have a class derived from QLabel that can elide[0] text (ElidedLabel). The list items should be selectable, so I'm using QListWidget to display them. An instance of said custom widget is then set as item widget for each QListWidgetItem.
The ElidedLabel is implemented as suggested here: Status bar and elided label

Problem:
The custom widget tends to be wider than the QListWidget displaying it. To make use of the ElidedLabel, the QListWidget should resize the custom widget to match its width so that text is elided. For some reason, this is not happening and the custom widget just gets clipped.

What I've tried:
I have put together a minimal example that demonstrates a dummy "custom widget" that elides fine as a child of an ordinary layout in the main window. The same widget does not properly elide as an item widget in the QListWidget.
The SizePolicies are set to Preferred everywhere, but I feel like I've played with everything else, too. The ElidedLabel has its minimumWidth set to 1 to allow for it to shrink. Layout constraints were left at their defaults. The QListWidget has ResizeMode set to Adjust.

SO won't let me post a screenshot, find it here: ht*ps://filetrain.de/elidetest.png

[0]: elide: cut off text that doesn't fit and replace it by an ellipsis ('...')

1

1 Answers

0
votes

Add these two methods to ElidedLabel

virtual QSize minimumSizeHint() const
{
    return QSize(0, QLabel::minimumSizeHint().height());
}
virtual QSize sizeHint() const
{
    return QSize(0, QLabel::sizeHint().height());
}