Results 1 to 3 of 3

Thread: problem with QListWidgetItem with text and icon

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2010
    Posts
    6
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default problem with QListWidgetItem with text and icon

    Hello EveryBody,

    I am new to the Qt community and says hello to all of you.



    I am trying to build an horizontal QListWidget. This has be done without problem

    the problem is with the QListWidetItem objects I want to insert.
    These QListWidgetItem contain text and icon and they are presented with the icon left an the text wright.
    I'd like to have QListWidgetItem with Icon above and text under the icon


    the source code is:

    SMListView::SMListView(QWidget *parent) : QWidget(parent)
    {
    m_listView = new QListWidget(this);
    m_listView->setFlow(QListView::LeftToRight);

    QListWidgetItem *item1 = new QListWidgetItem(QIcon("_2.BMP"), "elem1", m_listView, QListWidgetItem::UserType);
    QListWidgetItem *item2 = new QListWidgetItem(QIcon("_2.BMP"), "elem2", m_listView, QListWidgetItem::UserType);
    QListWidgetItem *item3 = new QListWidgetItem(QIcon("_2.BMP"), "elem3", m_listView, QListWidgetItem::UserType);
    QListWidgetItem *item4 = new QListWidgetItem(QIcon("_2.BMP"), "elem4", m_listView, QListWidgetItem::UserType);
    QListWidgetItem *item5 = new QListWidgetItem(QIcon("_2.BMP"), "elem5", m_listView, QListWidgetItem::UserType);

    item1->setTextAlignment(Qt::AlignCenter);
    item2->setTextAlignment(Qt::AlignCenter);
    item3->setTextAlignment(Qt::AlignCenter);
    item4->setTextAlignment(Qt::AlignCenter);
    item5->setTextAlignment(Qt::AlignCenter);
    }

    for example the item1 presentation is icon left and text wright
    I'd like to have icon above and the text under the icon


    How to do?

    I'd thank all developpers that have a solution for me.

    Best Regards
    Last edited by utopia500; 15th April 2010 at 15:39.

  2. #2
    Join Date
    Oct 2009
    Posts
    151
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 13 Times in 11 Posts

    Default Re: problem with QListWidgetItem with text and icon

    One way of doing this is to restrict the width of your QListWidgetItem so that the icon and text can not fit on the same line.
    This will force the text to appear below the icon.

    You could try using the QListWidgetItem->setSizeHint(), setting the width to slightly larger than that of the icon.

    Failing that you can set the QListWidget's maximum width or maybe use a layout.

  3. #3
    Join Date
    Jun 2010
    Location
    Espoo, Finland
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60 Maemo/MeeGo

    Default Re: problem with QListWidgetItem with text and icon

    Quote Originally Posted by utopia500 View Post
    Hello EveryBody,

    I am new to the Qt community and says hello to all of you.

    I am trying to build an horizontal QListWidget. This has be done without problem

    the problem is with the QListWidetItem objects I want to insert.
    These QListWidgetItem contain text and icon and they are presented with the icon left an the text wright.
    I'd like to have QListWidgetItem with Icon above and text under the icon
    You can do that easily. You can combine text alignments using OR-operator. So change these alignments:

    item1->setTextAlignment(Qt::AlignCenter);
    item2->setTextAlignment(Qt::AlignCenter);
    item3->setTextAlignment(Qt::AlignCenter);
    item4->setTextAlignment(Qt::AlignCenter);
    item5->setTextAlignment(Qt::AlignCenter);
    to these:

    item1->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    item2->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    item3->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    item4->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    item5->setTextAlignment(Qt::AlignHCenter | Qt::AlignBottom);

Similar Threads

  1. Rich Text Inside QListWidgetItem?
    By Jick in forum Newbie
    Replies: 1
    Last Post: 17th February 2010, 01:30
  2. Replies: 2
    Last Post: 15th July 2009, 15:53
  3. Text under the Icon
    By QiT in forum Newbie
    Replies: 3
    Last Post: 1st April 2007, 17:42
  4. QListWidgetItem Text Rect
    By js67257 in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2006, 12:02
  5. QListWidgetItem->text() causing application segfault
    By Valheru in forum Qt Programming
    Replies: 6
    Last Post: 21st August 2006, 22:08

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.