[Interest] QCombobox - resize list view to horizontal contents

Daniel Price daniel.price at fxhome.com
Mon Sep 3 18:20:59 CEST 2012

I have a Qt-Windows applicastion.

I have a non-editable QComboBox subclass with a custom QAbstractListModel driving it. I have a simple QListView subclass acting as the view for the combobox.

I need the list view (NOT the combo) to resize horizontally to fit the widest string in the list instead of being fixed to the width of the combo button.

Previously, I used a QListWidget subclass as a the view and overrode the sizeHint():

struct ComboListWidget : public QListWidget
                ComboListWidget(QWidget *parent = 0)

                virtual QSize sizeHint() const
                                QFontMetrics fm(font());

                                int maxWidth = 0;
                                for (int i=0; i < count(); ++i)
                                                QListWidgetItem *it = item(i);
                                                const QSize sz = fm.size(Qt::TextSingleLine, it->text());
                                                maxWidth = qMin(qMax(maxWidth, sz.width()), MaxListWidth);

                                QMargins mar = contentsMargins();
                                int hpad = (mar.left() + mar.right()) * 2;

                                return QSize(maxWidth + hpad, 0);

But this code was really expensive, particularly if the list contains dozens of entries.

As I'm now using a custom model, I tried to implement a custom SizeHintRole:

if (role == Qt::SizeHintRole)
                const QString &text = data(index, Qt::DisplayRole).value<QString>();

                QFontMetrics fontMetrics(data(index, Qt::FontRole).value<QFont>());
                //QSize size(fontMetrics.width(text), fontMetrics.height());
                return QSize(1000,20); // ignores the 1000
                //return size;

But the list view only uses the vertical size and I nothing I do seems to work. If I set the list view to a fixed width in it's constructor, it breaks the selection-highlight such that it only extends as far as the combobox's width.

This only seems to be a problem on Windows. On OSX, the list view expands correctly with no additional code.

There must be a way to do this - please help!

This email is confidential. It may also be privileged or otherwise protected by work product immunity or other legal rules. Errors and Omissions Excluded. If you are not the intended recipient please notify the sender. Please delete the message from all places in your computer where it is stored. You should not copy the email or use it for any purpose or disclose its contents to any other person. To do so may be unlawful. Email is an informal means of communicating and may be subject to data corruption accidentally or deliberately. For this reason it is inappropriate to rely on advice contained in an email without obtaining written confirmation of it first.

FXhome Limited is a limited company registered in England and Wales. Registered number: 04172812. Registered office: Suite 4 St Giles House, 27 St Giles Street, Norwich, Norfolk, NR2 1JN, U.K.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120903/e3f0312d/attachment.html>

More information about the Interest mailing list