Results 1 to 4 of 4

Thread: QListWidget add QListWidgetItem

  1. #1
    Join Date
    Feb 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget add QListWidgetItem

    My code seems right but doesn't display anything in my QListWidget. So, what am I doing wrong? Thanks for the help
    Qt Code:
    1. QListWidgetItem add1("item 1", 0, 1);
    2. QListWidgetItem add2("item 2", 0, 2);
    3. QListWidgetItem add3("item 3", 0, 3);
    4. QListWidgetItem add4("item 4", 0, 4);
    5. list_widget->addItem(&add1);
    6. list_widget->addItem(&add2);
    7. list_widget->addItem(&add3);
    8. list_widget->addItem(&add4);
    To copy to clipboard, switch view to plain text mode 

    What I think should happen is this: I would have a QListWidget (made from a form) that shows item 1 to item 4... I am trying to use the type as an ID so that if item 1 is a different language then I have the ID not the string.

    What happens is I don't see anything in my QListWidget (it is empty right after adding them all.) I am using QT 4.0
    Last edited by fellobo; 20th February 2006 at 19:51. Reason: code part looked wrong

  2. #2
    Join Date
    Feb 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: QListWidget add QListWidgetItem

    okay, so I figured it out... With the help of another QT programmer.

    You can't mess with the type (I am not too sure as to why but that is why it isn't working.)

    here is the solution we found:

    Qt Code:
    1. // make the ListWidgetItems, and assign them to the list_widget
    2. QListWidgetItem add1("item 1", list_widget);
    3. QListWidgetItem add2("item 2", list_widget);
    4. QListWidgetItem add3("item 3", list_widget);
    5. QListWidgetItem add4("item 4", list_widget);
    6.  
    7. // add a QVariant to the LIstWidgetItem
    8. add1.setData(Qt::UserRole, QVariant(1));
    9. add2.setData(Qt::UserRole, QVariant(2));
    10. add3.setData(Qt::UserRole, QVariant(3));
    11. add4.setData(Qt::UserRole, QVariant(4));
    12.  
    13. // to get that number back out
    14. list_widget->item(selected_list_widget)->data(Qt::UserRole).toInt());
    To copy to clipboard, switch view to plain text mode 

    Long live the QT

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListWidget add QListWidgetItem

    And this works? Hmm... seems odd, as you're allocating objects on the stack. They should get deleted upon returning from the function, causing those items to be removed from the list.

    This should work too:

    Qt Code:
    1. list_widget->addItem(new QListWidgetItem("item1"));
    2. list_widget->addItem(new QListWidgetItem("item2"));
    3. list_widget->addItem(new QListWidgetItem("item3"));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget add QListWidgetItem

    ah, yes, my apologies that code wasn't correct....
    Qt Code:
    1. // make the ListWidgetItems, and assign them to the list_widget
    2. QListWidgetItem *padd1 = new QListWidgetItem("item 1", list_widget);
    3. QListWidgetItem *padd2 = new QListWidgetItem("item 2", list_widget);
    4. QListWidgetItem *padd3 = new QListWidgetItem("item 3", list_widget);
    5. QListWidgetItem *padd4 = new QListWidgetItem("item 4", list_widget);
    6.  
    7. // add a QVariant to the LIstWidgetItem
    8. padd1->setData(Qt::UserRole, QVariant(1));
    9. padd2->setData(Qt::UserRole, QVariant(2));
    10. padd3->setData(Qt::UserRole, QVariant(3));
    11. padd4->setData(Qt::UserRole, QVariant(4));
    12.  
    13. // to get that number back out
    14. list_widget->item(selected_list_widget)->data(Qt::UserRole).toInt();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QListWidget and QListWidgetItem
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 3rd August 2011, 10:59
  2. Replies: 3
    Last Post: 25th July 2008, 15:30
  3. Check Box problem
    By Seema Rao in forum Qt Programming
    Replies: 6
    Last Post: 30th November 2007, 20:32
  4. Replies: 13
    Last Post: 15th December 2006, 12:52
  5. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 10:56

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.