×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Hyderabad
    Posts
    16

    Question How to add icons to QListWidget?

    Hi,

    I am using below code for displaying custom list for showing items.But how to add icons to that particular item in a particular place.I want to add 3 icons every item.So Anyone help me solve this problem.

    Thanks in advance.................


    I want list like this.Check this below image with listWidget



    class MyDelegate2_feed : public QStyledItemDelegate {
    public:
    MyDelegate2_feed(QObject *parent=0) : QStyledItemDelegate (parent){}

    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    if(option.state & QStyle::State_Selected){
    painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    }

    painter->drawRoundedRect(option.rect,20.0,15.0);
    // QRectF target(10.0, 20.0, 80.0, 60.0);
    // QRectF source(0.0, 0.0, 0.20, .20);

    // QImage image(":/Collaber_Logo.jpg");


    // painter->drawImage(option.rect,image,source);
    QRect r;

    QString title = index.data(Qt::UserRole + 5).toString();
    QString description = index.data(Qt::UserRole + 1).toString();
    QString comment = index.data(Qt::UserRole + 2).toString();
    QString status = index.data(Qt::UserRole + 3).toString();
    QString person = index.data(Qt::UserRole + 4).toString();

    r= option.rect.adjusted(10, 0, 0,0);
    r.setWidth(150);

    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignTop|Qt::AlignLeft, title, &r);
    r = option.rect.adjusted(10, 0, 0,0);
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignTop|Qt::AlignRight|Qt::TextWordWrap, person, &r);
    r = option.rect.adjusted(10, 30,0, 0);
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft|Qt::TextWordWrap, description, &r);

    r = option.rect.adjusted(30, 20, 0, 0);
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignCenter|Qt::TextWordWrap, comment, &r);

    r = option.rect.adjusted(30, 30, 0, 0);
    painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignRight|Qt::TextWordWrap, status, &r);
    }

    QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    return QSize(200, 70);
    }


    };

    Feed::Feed(QWidget *parent) :
    QWidget(parent)
    {
    listWidget=new QListWidget();

    connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(Ch(QListWidgetItem*)));
    listWidget->setItemDelegate(new MyDelegate2_feed(listWidget));
    for (int i = 0; i < 10; i++) {


    QListWidgetItem *item = new QListWidgetItem();


    //item->setData(Qt:isplayRole, "df");
    item->setData(Qt::UserRole + 5, "Comment");
    item->setData(Qt:isplayRole, "nandigama");
    item->setData(Qt::UserRole + 1, " OnTask");
    item->setData(Qt::UserRole + 2, "");
    item->setData(Qt::UserRole + 3, "3 hrs ago");
    item->setData(Qt::UserRole + 4, "By name");

    listWidget->addItem(item);

    }
    //Creating layout for all GUI controls

    QHBoxLayout *kk=new QHBoxLayout();
    kk->addWidget(listWidget);
    QVBoxLayout *main_v=new QVBoxLayout();
    main_v->addLayout(kk);
    setLayout(main_v);


    }
    Last edited by SaiPrasad2010; 2011-02-11 at 06:44.

  2. #2
    Registered User
    Join Date
    Feb 2007
    Location
    India
    Posts
    1,128

    Re: How to add icons to QListWidget?

    For icon...use
    list=new QListWidget();
    QListWidgetItem ( const QIcon & icon, const QString & text, QListWidget * parent = 0, int type = Type )
    iTem1 = new QListWidgetItem(QIcon("D:\\abc.PNG"),"xyz",list);

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Hyderabad
    Posts
    16

    Re: How to add icons to QListWidget?

    Hi,
    Thank you for quick reply.I want to place the image particular place in the listitem.

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to add icons to QListWidget?

    You probably need to create your own QWidget and place it with setItemWidget().

  5. #5
    Super Contributor
    Join Date
    Jul 2008
    Posts
    2,020

    Re: How to add icons to QListWidget?

    If you want a column of icons next to one another, try using a Table Widget.
    The lines above are the best I have to offer.If anyone of you is of more advanced knowledge, I ask for your patience and understanding! - unknown arab poet
    http://www.tamoggemon.com - Symbian blog - Windows Phone blog
    My other blogs:
    webOS blog iPhone blog BlackBerry blog Samsung bada blog Android blog

Similar Threads

  1. How to add more items in QListWidget
    By siddugh in forum [Archived] Qt SDKs and Tools
    Replies: 12
    Last Post: 2011-01-03, 05:23
  2. Replies: 6
    Last Post: 2010-12-03, 14:46
  3. Replies: 8
    Last Post: 2010-09-20, 22:13
  4. Replies: 2
    Last Post: 2008-12-04, 08:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×