Qt - use QListWidgetItem user data
From TaskDepender
(→Implementation) |
(→Implementation) |
||
Line 1: | Line 1: | ||
''This page describes how the user data of a QListWidgetItem can be used to store a custom class.'' | ''This page describes how the user data of a QListWidgetItem can be used to store a custom class.'' | ||
+ | |||
+ | == Introduction == | ||
+ | |||
+ | The <tt>QListWidgetItem</tt><ref>[http://developer.qt.nokia.com/doc/qt-4.8/qlistwidgetitem.html QListWidget Class Reference - Qt 4.8]</ref> has an attribute: | ||
+ | |||
+ | <syntaxhighlight lang="cpp-qt"> | ||
+ | QVariant QListWidgetItem data(int role) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | This can be used to store a custom class. How this is done is described on this page. | ||
== Custom class definition == | == Custom class definition == | ||
− | + | Suppose a custom class is defined as follows: | |
− | <syntaxhighlight lang="cpp"> | + | <syntaxhighlight lang="cpp-qt"> |
class MyCustomClass | class MyCustomClass | ||
{ | { | ||
public: | public: | ||
− | MyCustomClass() | + | MyCustomClass(); |
− | ~MyCustomClass() | + | ~MyCustomClass(); |
int a; | int a; | ||
}; | }; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | To ensure that the class is compatible with QVariant, the following is added<ref>[http://developer.qt.nokia.com/doc/qt-4.8//qmetatype.html QMetaType Class Reference - Qt 4.8 reference]</ref>: | ||
+ | |||
+ | <syntaxhighlight lang="cpp-qt"> | ||
+ | #include <QMetaType> | ||
+ | Q_DECLARE_METATYPE(MyCustomClass) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 21: | Line 38: | ||
== Adding the QListWidget == | == Adding the QListWidget == | ||
− | Suppose | + | Suppose one edit fields hold the value for <tt>a</tt> and the other the text associated with the list widget item. If a button is clicked, a new <tt>QListWidgetItem</tt> must be added with the text retrieve from one of the line edits and an instance of the <tt>MyCustomClass</tt> must be attached with the <tt>a</tt> attribute set by the integer conversion of the other line edit field. |
When clicked on an item in the list, the date of the item clicked must be received and put in the corresponding line edit fields. | When clicked on an item in the list, the date of the item clicked must be received and put in the corresponding line edit fields. | ||
Line 27: | Line 44: | ||
== Implementation == | == Implementation == | ||
− | <syntaxhighlight lang="cpp"> | + | TODO: create actual small application. |
− | void MainWindow:: | + | |
+ | === Add item === | ||
+ | |||
+ | <syntaxhighlight lang="cpp-qt"> | ||
+ | void MainWindow::addItem() | ||
{ | { | ||
+ | // Local temporary variables. | ||
MyCustomClass myClass; | MyCustomClass myClass; | ||
QVariant qv; | QVariant qv; | ||
myClass.a = ui->valueLineEdit->text().toInt(); | myClass.a = ui->valueLineEdit->text().toInt(); | ||
+ | |||
+ | // Use content of local variable and copy. | ||
qv.setValue(myClass); | qv.setValue(myClass); | ||
Line 39: | Line 63: | ||
qlwi->setText(ui->textLineEdit->text()); | qlwi->setText(ui->textLineEdit->text()); | ||
+ | // Copy the local qvariant and store with list item. | ||
qlwi->setData(Qt::UserRole,qv); | qlwi->setData(Qt::UserRole,qv); | ||
ui->listWidget->addItem(qlwi); | ui->listWidget->addItem(qlwi); | ||
Line 44: | Line 69: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | === Display data === | ||
− | + | <syntaxhighlight lang="cpp-qt"> | |
− | <syntaxhighlight lang="cpp"> | + | void MainWindow::displayData(QListWidgetItem *item) |
− | void MainWindow:: | + | |
{ | { | ||
− | textLineEdit->setText( | + | textLineEdit->setText(item->text()); |
− | MyCustomClass myClass = | + | MyCustomClass myClass = item->data(Qt::UserRole).value<MyCustomClass>(); |
ui->lineEdit->setText(QString("%1").arg(myClass.a)); | ui->lineEdit->setText(QString("%1").arg(myClass.a)); | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Change data === | ||
+ | |||
+ | <syntaxhighlight lang="cpp-qt"> | ||
+ | void MainWindow::changeData(QListWidgetItem *item) | ||
+ | { | ||
+ | |||
+ | QVariant *qvPtr = item->data(Qt::UserRole); | ||
+ | |||
+ | MyCustomClass myClass = qvPtr->value<MyCustomClass>(); | ||
+ | |||
+ | myClass.a += 1; | ||
+ | |||
+ | qvPtr->setValue(myClass); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == References == | ||
+ | <references/> |