Results 1 to 4 of 4

Thread: populated QListView with QString

  1. #1
    Join Date
    Apr 2010
    Posts
    7
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Question populated QListView with QString

    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..
    Qt Code:
    1. QStringList listing;
    2. QListView ListPic;
    3.  
    4. for (int i = 0; i < listing.size(); i++)
    5. {
    6. QString temp = listing.at(i);
    7. ui->ListPic->currentIndex().data() = temp;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for the help

  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: populated QListView with QString

    Why dont you use QListWidget instead ?

  3. The following user says thank you to aamer4yu for this useful post:

    geleven (7th April 2010)

  4. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: populated QListView with QString

    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:
    Qt Code:
    1. QStringList listing;
    2. QStringListModel *model = new QStringListModel(/*some QObject * as a parent, e.g. ui->ListPic */ ui->ListPic);
    3. model->setStringList(listing);
    4. ui->ListPic->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    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.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following user says thank you to faldzip for this useful post:

    geleven (7th April 2010)

  6. #4
    Join Date
    Apr 2010
    Posts
    7
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: populated QListView with QString

    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..

Similar Threads

  1. Can QListBox be populated by custom Widgets?
    By qtUser500 in forum Qt Tools
    Replies: 1
    Last Post: 26th February 2009, 14:14
  2. Replies: 4
    Last Post: 31st January 2008, 20:44
  3. Q3ListView not being populated.
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 19th March 2007, 15:54
  4. QHash only getting updated not populated
    By quickNitin in forum Newbie
    Replies: 2
    Last Post: 13th July 2006, 11:32
  5. container populated with custom widgets
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 16th April 2006, 21:02

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.