PDA

View Full Version : Add Items to QListView



gaganbm
30th November 2012, 12:44
Hi,

I am a complete beginner to Qt. Please pardon my stupid question!

I found many threads and examples related to this but still I am unable to clear it out. I have a listView created by Qt Creator. And I want to add items to the list just like the one figure I am attaching here. Every row consists of one small icon and a text label.

8469

How do I do that ?

Any help would be appreciated.

Thanks

Added after 42 minutes:

Anyways, this thing worked for me :
http://www.qtcentre.org/threads/33196-QListView?p=153860#post153860

Now trying to set the proper icon size.

steadi
1st December 2012, 20:14
Hey Man,

I am not the best at C++ but this is how I would go about it...

Firstly, create a class for each of your QListItem's by using the following:


class MyListItem : QListWidgetItem
{
public:
QPixmap *icon_to_be_shown;
};


Now you will want to create the QListWidget itself and also create your actual item:


void Main()
{
//Create Our QListWidget
QListWidget *mylist = new QListWidget;

//Create Our Item
MyListItem *itm = new MyListItem;
itm->setText("My Item");
itm->icon_to_be_shown = "PATH TO PICTURE";

//Add Our Item To Our List
mylist->addItem(itm);
}


Hope this bit of code helps man, also you should look into QListWidget as oppose to QList =)
Matt

amleto
1st December 2012, 20:25
no need to subclass QListWidgetItem.


Please note the difference between QListWIDGET and QListVIEW! Check the docs to see which one you require.

steadi
1st December 2012, 20:28
no need to subclass QListWidgetItem
How would he then go about assigning images to the items? or am i just overlooking something as usual xD

amleto
1st December 2012, 22:56
for starters, you should ask yourself how Qt would know what to do with your 'pointer = string literal'? Your solution won't do anything, even if it would compile... How does the list magically know about this pixmap pointer that you have added?



QPixmap *icon_to_be_shown;
icon_to_be_shown = "PATH TO PICTURE";

That doesn't make sense at all.

The docs are, as per usual, useful:
qlistwidgetitem.html#setIcon

steadi
1st December 2012, 23:28
for starters, you should ask yourself how Qt would know what to do with your 'pointer = string literal'? Your solution won't do anything, even if it would compile... How does the list magically know about this pixmap pointer that you have added?



QPixmap *icon_to_be_shown;
icon_to_be_shown = "PATH TO PICTURE";

That doesn't make sense at all.

The docs are, as per usual, useful:
qlistwidgetitem.html#setIcon

Awww shit man, sorry about that, cannot believe that I completely overlooked the ->setIcon xD
Thanks man,
Matt