PDA

View Full Version : Auto-expand combo box to contents on windows



louis_xx
13th June 2008, 17:08
I am using a combo box (custom delegate) and on the Mac it seems to expand nicely to display the contents. However on windows the combo box seems to be the width of the cell and chops off the contents.

How can i get the combo box on windows to display strings of arbitrary length?

jpn
13th June 2008, 19:02
Which size adjust policy (see QComboBox docs for more details) are you using?

blivrail
11th February 2009, 20:15
I have a similar problem. I would like to see the popup wider so that long texts are not replaced with (...).

I've tried using
QComboBox::setSizeAdjustPolicy( QComboBox::AdjustToContents ) to do this, and it works as intended. But, in the context of a QComboBox as a delegate for a table view, this does not work well. The display widget and popup both expand in size, and when the editing cell is the right most, the down arrow goes off the table.

Is there any way to only expand the popup but not the display widget?

Thanks!

talk2amulya
12th February 2009, 08:04
here is an example to do it:


int listwidth = 0;
QListView* pView = (QListView*)view();

for (int i = 0; i < count() - 1; i++)
{
listwidth = qMax(itemText(i).length(), itemText(i+1).length());
}

listwidth = qMax(sizeHint().width(), listwidth);

QDesktopWidget* desktop = QApplication::desktop();
QRect availRect = desktop->availableGeometry(this->parentWidget());
int maxWidth = (availRect.right())-(this->mapToGlobal(QPoint(0, 0)).x())-10;

listwidth = qMin(maxWidth, listwidth);
pView->setMinimumWidth(listwidth );

here, u get the view from combobox, find the maximum length of all the items and maximum width according to the parent of combobox..compare it and set the minimum width of the view

blivrail
12th February 2009, 21:23
@talk2amulya: Thanks for your input. I was working a solution like this, but I tried to use QWidget::resize() to set the new size. The popup kept on getting clipped on the right hand size. Instead, using setMinimumWidth() as you suggested, it worked fine!

I also used QFontMetric to find the text width instead of QString::length().