Results 1 to 6 of 6

Thread: Add Items to QListView

  1. #1
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    5

    Default Re: Add Items to QListView

    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.

    example.jpg

    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/3319...860#post153860

    Now trying to set the proper icon size.
    Last edited by gaganbm; 30th November 2012 at 13:44.

  2. #2
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add Items to QListView

    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:
    Qt Code:
    1. class MyListItem : QListWidgetItem
    2. {
    3. public:
    4. QPixmap *icon_to_be_shown;
    5. };
    To copy to clipboard, switch view to plain text mode 

    Now you will want to create the QListWidget itself and also create your actual item:
    Qt Code:
    1. void Main()
    2. {
    3. //Create Our QListWidget
    4. QListWidget *mylist = new QListWidget;
    5.  
    6. //Create Our Item
    7. MyListItem *itm = new MyListItem;
    8. itm->setText("My Item");
    9. itm->icon_to_be_shown = "PATH TO PICTURE";
    10.  
    11. //Add Our Item To Our List
    12. mylist->addItem(itm);
    13. }
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add Items to QListView

    no need to subclass QListWidgetItem.


    Please note the difference between QListWIDGET and QListVIEW! Check the docs to see which one you require.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add Items to QListView

    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

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add Items to QListView

    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?

    Qt Code:
    1. QPixmap *icon_to_be_shown;
    2. icon_to_be_shown = "PATH TO PICTURE";
    To copy to clipboard, switch view to plain text mode 
    That doesn't make sense at all.

    The docs are, as per usual, useful:
    http://doc.qt.io/qt-4.8/qlistwidgetitem.html#setIcon
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Oct 2012
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add Items to QListView

    Quote Originally Posted by amleto View Post
    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?

    Qt Code:
    1. QPixmap *icon_to_be_shown;
    2. icon_to_be_shown = "PATH TO PICTURE";
    To copy to clipboard, switch view to plain text mode 
    That doesn't make sense at all.

    The docs are, as per usual, useful:
    http://doc.qt.io/qt-4.8/qlistwidgetitem.html#setIcon
    Awww shit man, sorry about that, cannot believe that I completely overlooked the ->setIcon xD
    Thanks man,
    Matt

Similar Threads

  1. QListView Items with Buttons
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2010, 17:48
  2. How to reorder items in QListView?
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2009, 17:09
  3. Removing all items QListView
    By vcp in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2009, 19:55
  4. Rendering items on QListView
    By rcintra in forum Qt Programming
    Replies: 6
    Last Post: 10th May 2007, 23:22
  5. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 13:25

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.