Results 1 to 12 of 12

Thread: setting size of a QListWidget to the width of its largest child

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default setting size of a QListWidget to the width of its largest child

    Hi,
    I am writing a preferences dialog that has a QListWidget on the left side and the settings buried in QTabWidgets and QWidgets positioned to the right of the listwidget. Now, when I open the prefs dialog, it divides the size of the dialog equally between the two widgets, but that is not what I want. I want the size of the QListWidget to be the size of the maximum QListWidgetItem it contains + a few percent. and resizing the dialog should give every extra space to the QTabWidgets/QWidgets. I can achieve this by setting the QListWidgets size to some static value like "120". This however should be problematic, if someone uses larger fonts etc. iterating through the QListWidgetItems and getting the maximum width by using QFontMetrics is also not the best solution since the outcome is rendered useless if I add a QIcon to the QListWidgetItem.

    Qt Code:
    1. // set Size
    2. QFontMetrics fm( font() );
    3. int maxTextWidth;
    4.  
    5. for (int i=0; i<listWidget->count(); i++) {
    6. qDebug() << listWidget->item(i)->sizeHint().width();
    7. int x;
    8. if ( (x = fm.width(listWidget->item(i)->text())) > maxTextWidth )
    9. maxTextWidth = x;
    10. };
    11. listWidget->setMaximumWidth( maxTextWidth + maxTextWidth * 0.3 ); // add 30 percent to the size
    To copy to clipboard, switch view to plain text mode 

    The Items sizeHint() function returns -1 so I can't use it either. Can anyone tell me how to do it the right way?

    Thanx in advance
    momesana

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setting size of a QListWidget to the width of its largest child

    You should use layouts for that, a QHBoxLayout sound good for your case.
    Also I don't think using setMaximumWidth for the QListWidget is good, since it only says what is the alowed maximum width, it will not neseseraly be that wide.
    I'd use setWidth() for that.

  3. #3
    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: setting size of a QListWidget to the width of its largest child

    Quote Originally Posted by momesana View Post
    The Items sizeHint() function returns -1 so I can't use it either.
    QListWidgetItem::sizeHint() returns the size hint which has been set. So if no size hint has been explicitly set, an invalid size hint is returned. You may ask the delegate to calculate a proper size hint for each item.

    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: setting size of a QListWidget to the width of its largest child

    Quote Originally Posted by jpn View Post
    QListWidgetItem::sizeHint() returns the size hint which has been set. So if no size hint has been explicitly set, an invalid size hint is returned. You may ask the delegate to calculate a proper size hint for each item.

    I tried to follow your instructions and wrote this:
    Qt Code:
    1. // set Size
    2. int maxItemWidth = 0;
    3.  
    4. for (int i=0; i<listWidget->count(); i++) {
    5.  
    6. QModelIndex index = listWidget->indexFromItem(listWidget->item(i));
    7. QAbstractItemDelegate * delegate = listWidget->itemDelegate();
    8. QSize size = delegate->sizeHint( QStyleOptionViewItem (), index );
    9.  
    10. int x;
    11. if ( ( x = size.width() ) > maxItemWidth )
    12. maxItemWidth = x;
    13. };
    14. listWidget->setMaximumWidth( maxItemWidth );
    To copy to clipboard, switch view to plain text mode 
    Now, the compiler complains about listWidget::indexFromItem being protected... Do I really have to subclass the listWidget to be able to get the ModelIndex or is there a simpler way to achieve this?

    Thanx in advance
    momesana

  5. #5
    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: setting size of a QListWidget to the width of its largest child

    Quote Originally Posted by momesana View Post
    Now, the compiler complains about listWidget::indexFromItem being protected... Do I really have to subclass the listWidget to be able to get the ModelIndex or is there a simpler way to achieve this?
    To get a model index, yes, you "have to". Why are people always so afraid of subclassing...
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: setting size of a QListWidget to the width of its largest child

    Quote Originally Posted by high_flyer View Post
    You should use layouts for that, a QHBoxLayout sound good for your case.
    Also I don't think using setMaximumWidth for the QListWidget is good, since it only says what is the alowed maximum width, it will not neseseraly be that wide.
    I'd use setWidth() for that.
    A layout? Within a QListWidget? Afaik, QListWidgetItems cannot be put into a Layout anyway, since they do not inherit QWidget. http://doc.trolltech.com/4.2/hierarchy.html

    Anyways, there is no setWidth() function that I could call on a QListWidget. The only possible values are setMinimumWidth or setMaximumWidth. setting both would be a solution here as well as setting setFixedWidth().

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: setting size of a QListWidget to the width of its largest child

    Quote Originally Posted by jpn View Post
    To get a model index, yes, you "have to". Why are people always so afraid of subclassing...
    I subclassed it. The value returned by the delegates sizeHint() however also does not take into consideration the QIcon that I've added to the QWidgetItem. The effekt is the same as using a QFontMetric object to calculate the width of the item

    Any further ideas?

    Thanx
    momesana

  8. #8
    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: setting size of a QListWidget to the width of its largest child

    You will have to pass a proper QStyleOptionViewItem. Try this:
    Qt Code:
    1. QSize size = delegate->sizeHint(viewOptions(), index);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  9. The following user says thank you to jpn for this useful post:

    momesana (22nd September 2006)

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setting size of a QListWidget to the width of its largest child

    A layout? Within a QListWidget? Afaik, QListWidgetItems cannot be put into a Layout anyway, since they do not inherit QWidget.
    Oh?
    Lets see:
    QListWidget (the name already suggestes that it is a QWidget) inhertis QListView which inherits QAbstractItemView which inherits QAbstractScrollArea which inhertis QFrame which inherits QWidget.
    Which by the way is exactly what you see in the link you posted.

    So I don't see why not have a widget in a layout, specially when you want to implement a sizing policy on it.
    Anyways, there is no setWidth() function that I could call on a QListWidget.
    You are right about that, I didn't check it, I remembered wrong.
    But my point stays the same, I'd use then setMinimumWidth() not maximum, since you want to assuere at least the width of the longest item in the list.

  11. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: setting size of a QListWidget to the width of its largest child

    I see where we missundertood each otoher.
    I meant to put the QListWidget in to a Layout with the other widgest in your dialog, to have them use always the relative size (size policy) to each other.
    I didn't understand that you were talking about QListWidgetItems in the QListWidget it self.
    (sorry this was meant to be an EDIT in the previous post)

  12. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: setting size of a QListWidget to the width of its largest child

    Quote Originally Posted by high_flyer View Post
    I see where we missundertood each otoher.
    I meant to put the QListWidget in to a Layout with the other widgest in your dialog, to have them use always the relative size (size policy) to each other.
    I didn't understand that you were talking about QListWidgetItems in the QListWidget it self.
    (sorry this was meant to be an EDIT in the previous post)
    no problem. Big thanks for taking the time to read and answer to my post in the first place.

  13. #12
    Join Date
    Feb 2007
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setting size of a QListWidget to the width of its largest child

    I had to fix the same problem. I ended up implementing a function to compute the minimum width of a list view, and then constraining the view using setFixedWidth(myComputedWidth).

    Here is the code of my function:
    Qt Code:
    1. int computeListViewMinimumWidth(QAbstractItemView* view) {
    2. int minWidth = 0;
    3. QAbstractItemModel* model = view->model();
    4.  
    5. // Too bad view->viewOptions() is protected
    6. // FIXME: Handle the case where text is below icon
    7. option.decorationSize = view->iconSize();
    8.  
    9. int rowCount = model->rowCount();
    10. for (int row = 0; row<rowCount; ++row) {
    11. QModelIndex index = model->index(row, 0);
    12. QSize size = view->itemDelegate()->sizeHint(option, index);
    13. minWidth = qMax(size.width(), minWidth);
    14. }
    15. minWidth += 2 * view->frameWidth();
    16. return minWidth;
    17. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by aurelien; 21st February 2007 at 11:36.

Similar Threads

  1. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.