[Interest] QCombobox - resize list view to horizontal contents
Daniel Price
daniel.price at fxhome.com
Thu Oct 4 12:09:28 CEST 2012
In case anyone else has this problem, I figured it out.
The call to setView() on a QComboBox resets the size policies on the view passed in. So any customizations within a subclass constructor WILL BE IGNORED. There's no hint of this in the documentation and I had to dig through the Qt source code to figure out what was going on.
The solution is to set the size policy of the custom view AFTER passing it to setView().
From: interest-bounces+daniel.price=fxhome.com at qt-project.org [mailto:interest-bounces+daniel.price=fxhome.com at qt-project.org] On Behalf Of Daniel Price
Sent: 05 September 2012 16:18
To: interest at qt-project.org
Subject: Re: [Interest] QCombobox - resize list view to horizontal contents
*bump*
Anyone? There must be a way to do this.
From: interest-bounces+daniel.price=fxhome.com at qt-project.org<mailto:interest-bounces+daniel.price=fxhome.com at qt-project.org> [mailto:interest-bounces+daniel.price=fxhome.com at qt-project.org] On Behalf Of Daniel Price
Sent: 03 September 2012 17:21
To: interest at qt-project.org<mailto:interest at qt-project.org>
Subject: [Interest] QCombobox - resize list view to horizontal contents
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)
:
QListWidget(parent)
{}
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/20121004/7b75ace2/attachment.html>
More information about the Interest
mailing list