Results 1 to 6 of 6

Thread: QListWidget width

  1. #1
    Join Date
    Mar 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QListWidget width

    Hi

    What, precisely, controls the default width of a QListWidget. I want a list widget that makes itself exactly wide enough to display the width of its widest item but no more.

    E.g. I would have expected the following simple code to produce a dialog with a list box exactly wide enough to hold the items "fred" and "bloggs" but no more. Yet the dialog that gets shown has a list widget some 5 times wider than necessary. Why?

    Qt Code:
    1. QDialog dlg(this);
    2. QListWidget* pListWidget = new QListWidget();
    3. new QListWidgetItem("Fred", pListWidget);
    4. new QListWidgetItem("Bloggs", pListWidget);
    5. QVBoxLayout* pLayout = new QVBoxLayout();
    6. pLayout->addWidget(pListWidget);
    7. dlg.setLayout(pLayout);
    8. dlg.exec();
    To copy to clipboard, switch view to plain text mode 

    What is the simplest, clean and reliable way to control the list width width without relying on pixel width constants. Code needs to be properly portable. Thanks
    Attached Images Attached Images

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget width

    Try setting different size policies and see if it works for you...
    (QSizePolicy::Minimum)

  3. #3
    Join Date
    Mar 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListWidget width

    Thanks. But I had already tried setting horz size policy on the QListWidget to Minimum. Makes no difference. Specifically I used:

    Qt Code:
    1. QSizePolicy sp = pListWidget->sizePolicy();
    2. sp.setHorizontalPolicy(QSizePolicy::Minimum);
    3. pListWidget->setSizePolicy(sp);
    To copy to clipboard, switch view to plain text mode 

    Any other ideas?

  4. #4
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget width

    Have you tried setMinimumWidth() and setMaximumWidth? I just made this up on the fly, so it might not compile, but it should look something like this.

    Qt Code:
    1. QDialog dlg(this);
    2. QListWidget* pListWidget = new QListWidget();
    3. QListWidgetItem fred = new QListWidgetItem("Fred", pListWidget);
    4. QListWidgetItem bloggs = new QListWidgetItem("Bloggs", pListWidget);
    5. QVBoxLayout* pLayout = new QVBoxLayout();
    6. pLayout->addWidget(pListWidget);
    7. dlg.setLayout(pLayout);
    8. dlg->setMinimumWidth(fred->sizeHint().width()+bloggs->sizeHint().width());
    9. dlg->setMaximumWidth(fred->sizeHint().width()+bloggs->sizeHint().width());
    10. dlg.exec();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListWidget width

    Thanks for the suggestion. But calling sizeHint().width() on the list widget items returns -1 which doesnt seem to help!

  6. #6
    Join Date
    Jun 2010
    Posts
    31
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget width

    Hehe, moment of stupidity. Fred and Bloggs are being handled by your pListWidget, so that's the size you care about.
    These things always happen to me on Monday mornings...


    Qt Code:
    1. QDialog dlg(this);
    2. QListWidget* pListWidget = new QListWidget();
    3. new QListWidgetItem("Fred", pListWidget);
    4. new QListWidgetItem("Bloggs", pListWidget);
    5. QVBoxLayout* pLayout = new QVBoxLayout();
    6. pLayout->addWidget(pListWidget);
    7. dlg.setLayout(pLayout);
    8. dlg->setMinimumWidth(pListWidget->size());
    9. dlg->setMaximumWidth(pListWidget->size());
    10. dlg.exec();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QToolBox width fixed to maximum content width
    By ghorwin in forum Qt Programming
    Replies: 0
    Last Post: 10th July 2009, 09:58
  2. How to set QTableView width to width of horizontal header?
    By martinb0820 in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 20:51
  3. Replies: 11
    Last Post: 21st February 2007, 11:26
  4. width()
    By mickey in forum Newbie
    Replies: 7
    Last Post: 27th July 2006, 15:18
  5. width()
    By mickey in forum Newbie
    Replies: 1
    Last Post: 1st April 2006, 00:34

Tags for this Thread

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.