Results 1 to 5 of 5

Thread: Auto-expand combo box to contents on windows

  1. #1
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Auto-expand combo box to contents on windows

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Auto-expand combo box to contents on windows

    Which size adjust policy (see QComboBox docs for more details) are you using?
    J-P Nurmi

  3. #3
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-expand combo box to contents on windows

    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
    Qt Code:
    1. QComboBox::setSizeAdjustPolicy( QComboBox::AdjustToContents )
    To copy to clipboard, switch view to plain text mode 
    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!

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Auto-expand combo box to contents on windows

    here is an example to do it:

    Qt Code:
    1. int listwidth = 0;
    2. QListView* pView = (QListView*)view();
    3.  
    4. for (int i = 0; i < count() - 1; i++)
    5. {
    6. listwidth = qMax(itemText(i).length(), itemText(i+1).length());
    7. }
    8.  
    9. listwidth = qMax(sizeHint().width(), listwidth);
    10.  
    11. QDesktopWidget* desktop = QApplication::desktop();
    12. QRect availRect = desktop->availableGeometry(this->parentWidget());
    13. int maxWidth = (availRect.right())-(this->mapToGlobal(QPoint(0, 0)).x())-10;
    14.  
    15. listwidth = qMin(maxWidth, listwidth);
    16. pView->setMinimumWidth(listwidth );
    To copy to clipboard, switch view to plain text mode 

    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

  5. #5
    Join Date
    Apr 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto-expand combo box to contents on windows

    @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().

Similar Threads

  1. Replies: 6
    Last Post: 18th August 2006, 17:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.