Results 1 to 5 of 5

Thread: Some Questions about QListWidget

  1. #1
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Some Questions about QListWidget

    In CSharp its as simple as writting :
    Qt Code:
    1. listBox1.Items.Add("Hello");
    2. listBox1.Items.Add("There");
    3.  
    4. foreach (string item in listBox1.Items )
    5. {
    6. MessageBox.Show(item.ToString());
    7. }
    To copy to clipboard, switch view to plain text mode 

    and i can easily add different objects to a list box and then retrieve them using foreach. I tried the same approach in Qt 4.8.2 but it seems they are different.though they look very similar at first.I found that Qt supports foreach so i went on and tried something like :
    Qt Code:
    1. foreach(QListWidgetItem& item,ui->listWidget->items())
    2. {
    3. item.setTextColor(QColor::blue());
    4. }
    To copy to clipboard, switch view to plain text mode 

    which failed clearly.It says the items() needs a parameter which confuses me.I am trying to iterate through the ListBox itself, so what does this mean? I tried passing the ListBox object as the parameter itself this again failed too:
    Qt Code:
    1. foreach(QListWidgetItem& item,ui->listWidget->items(ui->listWidget))
    2. {
    3. item.setTextColor(QColor::blue());
    4. }
    To copy to clipboard, switch view to plain text mode 

    So here are my questions:

    • How can I iterate through a QListWidget items in Qt?
    • Can i store objects as items in QListWidgets like C#?
    • How can i convert an object in QListWidgets to string(C#s ToString counter part in Qt) ?



    (suppose i want to use a QMessagBox instead of that setTextColor and want to print out all string items in the QlistWidget.)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Some Questions about QListWidget


    Warning... code off top of head:
    Qt Code:
    1. for (int i = 0; i < listWidget->count(); ++i) {
    2. listWidget->item(i)->setForeground(Qt::yellow);
    3.  
    4. qDebug() << listWidget->item(i)->text();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I am not sure the second part of your question means. The QListWidget contains a list of pointers to QListWidgetItem objects. You can subclass QListWidgetItem to do something special and insert pointers to subclass objects. You can also store any data that can fit into a QVariant against a Qt::UserRole using QListWidgetItem::setData() and QListWidgetItem::::data().
    Last edited by ChrisW67; 1st September 2012 at 06:12.

  3. #3
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Some Questions about QListWidget

    Quote Originally Posted by ChrisW67 View Post
    Warning... code off top of head:
    Qt Code:
    1. for (int i = 0; i < listWidget->count(); ++i) {
    2. listWidget->item(i)->setForeground(Qt::yellow);
    3.  
    4. qDebug() << listWidget->item(i)->text();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I am not sure the second part of your question means. The QListWidget contains a list of pointers to QListWidgetItem objects. You can subclass QListWidgetItem to do something special and insert pointers to subclass objects. You can also store any data that can fit into a QVariant against a Qt::UserRole using QListWidgetItem::setData() and QListWidgetItem::::data().
    Thank you very much.
    By my second question i meant, Is there a way that i can store refrences to any other objects rather than just simple string texts?
    and you forgot to answer my third question on C#s Tostring() method counter part in QT.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Some Questions about QListWidget

    Quote Originally Posted by Hossein View Post
    By my second question i meant, Is there a way that i can store refrences to any other objects rather than just simple string texts?
    http://doc-snapshot.qt-project.org/5...m.html#setData

    and you forgot to answer my third question on C#s Tostring() method counter part in QT.
    http://doc-snapshot.qt-project.org/5....html#toString
    You can't turn an elephant into a giraffe. The object you want to "convert" to a string needs to be able to. It's ambiguous. This means that in most cases, you need to provide a function that does this. I'm not sure what C# ToString() does or is applicable to. Do you want to save the contents of a listview as a textfile? Then you need to write that function yourself.

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

    Hossein (1st September 2012)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Some Questions about QListWidget

    Quote Originally Posted by Hossein View Post
    Thank you very much.
    By my second question i meant, Is there a way that i can store refrences to any other objects rather than just simple string texts?
    and you forgot to answer my third question on C#s Tostring() method counter part in QT.
    I answered all parts of your question... I did not 'forget' to answer anything.

    My last paragraph gives you two different ways to extend what is stored as an item in the list widget: extend the QListWidgetItem or store arbitrary pieces of data in user roles against the item. You can store a pointer or other handle happily in a QVariant.

    Line 4 of my example code shows how to do the equivalent of what I assume C#'s ToString() does: it gives you a string value of the specified list item (Qt::DisplayRole to be precise). The standard item can also have icon, colour, font, alignment, check state, and help information accessible through the data() function. If you use a user role to store custom information then you can use that to access any "toString()" functionality that might be associated with that custom information.

Similar Threads

  1. Some Questions
    By vimal in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2012, 17:36
  2. Replies: 2
    Last Post: 1st April 2011, 09:32
  3. 2 little questions
    By kib2 in forum Qt Tools
    Replies: 5
    Last Post: 25th October 2008, 16:25
  4. 2 Questions about QListWidget
    By SkripT in forum Qt Programming
    Replies: 3
    Last Post: 23rd March 2006, 10:35

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.