PDA

View Full Version : populated QListView with QString



geleven
6th April 2010, 12:48
Dear all,

i want to ask, how we can populate a QListView with item which is read from a QString?

my code below didn't work..


QStringList listing;
QListView ListPic;

for (int i = 0; i < listing.size(); i++)
{
QString temp = listing.at(i);
ui->ListPic->currentIndex().data() = temp;
}


Thank you for the help

aamer4yu
6th April 2010, 13:24
Why dont you use QListWidget instead ?

faldzip
6th April 2010, 14:40
Read about Model/View Programming in QtAssistant because you are missing the point. All *View classes are only views for representing some data. This data must be provided by a model. So if you have a view, then you need to create a model which can contain data (like in your case - it can have some QStringList or anything to store data inside) or it can be only interface to data (for example data are in some remote database and model is the thing which communicates with DB and exposes data to view).

In your case you have two solutions:
1. As mentioned - use QListWidget - it is "item based" not "model based" so you don't need any model, you need to add items (QListWidgetItem) to it. And give your data to those items.
2. Another ultra-easy solution :] - use QListView with some model - and there is one model which should done what you want: QStringListModel

Here is the example:


QStringList listing;
QStringListModel *model = new QStringListModel(/*some QObject * as a parent, e.g. ui->ListPic */ ui->ListPic);
model->setStringList(listing);
ui->ListPic->setModel(model);

That's it. But remember to read about Model/View programming to understand clearly how it works. There are many examples and really good description in Assistant.

geleven
7th April 2010, 16:05
ah, thanks you for all your help! since i new to Qt, i often get confused how to choose the right method to implement things
Something like QListWidget and QListView really confused me..