Results 1 to 4 of 4

Thread: Associating a string with a QListWidgetItem

  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Associating a string with a QListWidgetItem

    I want to write a simple program to let users create a list of notes.
    I've got a QListWidget and a QPlainTextEdit and a button to allow new list items to be added.
    Whenever a new item is added, I want to store the 'plain text' associated with the previous item.

    Is there as simple way to link a QString pointer to a given QListWidgetItem? Or do I need to create another 'list' (say, an array of QString pointers) and match it to the QListWidget rows (the first item in my array would implicitly be the QString for the first QListWidge row)?

  2. #2
    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: Associating a string with a QListWidgetItem

    Why QString pointers?

    Take a look at:
    http://doc.qt.nokia.com/4.7/qlistwid...m.html#setData

  3. #3
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Associating a string with a QListWidgetItem

    Or just QStrings. Either way. I was thinking pointers because I might need to change the string, but I guess I can just clear() and append() to the QString.
    I had a look at setData(), but I'm having trouble with the code.

    Basically I defined a role as:
    #define USER_ROLE_TEXT 1001;

    and then tried to pass and retrieve my string, but I was getting some strange errors. Could you provide an example code line of reading and writing a string to the list item?

  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: Associating a string with a QListWidgetItem

    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QListWidgetItem>
    6.  
    7. namespace Ui {
    8. class Widget;
    9. }
    10.  
    11. class Widget : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Widget(QWidget *parent = 0);
    17. ~Widget();
    18.  
    19. public slots:
    20. void showNote(QListWidgetItem*);
    21.  
    22. private:
    23. Ui::Widget *ui;
    24. };
    25.  
    26. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9.  
    10. connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(showNote(QListWidgetItem*)));
    11.  
    12. item->setText("Item 1");
    13. item->setData(1001, QString("This is the note that belongs to item 1"));
    14. ui->listWidget->addItem(item);
    15.  
    16. item = new QListWidgetItem;
    17. item->setText("Item 2");
    18. item->setData(1001, QString("Another note. One that belongs to item 2"));
    19. ui->listWidget->addItem(item);
    20. }
    21.  
    22. Widget::~Widget()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void Widget::showNote(QListWidgetItem *item)
    28. {
    29. ui->plainTextEdit->setPlainText(item->data(1001).toString());
    30. }
    To copy to clipboard, switch view to plain text mode 

    plainTextEdit and listWidget are added via designer.

Similar Threads

  1. Replies: 5
    Last Post: 5th August 2009, 17:32
  2. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 08:51
  3. Int to String - manipulating string
    By mickey in forum General Programming
    Replies: 6
    Last Post: 5th November 2007, 20:11
  4. Associating icon with a file type
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2006, 16:57
  5. Associating file extensions
    By fullmetalcoder in forum General Programming
    Replies: 9
    Last Post: 10th May 2006, 13:12

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.