Qt - use QListWidgetItem user data
From TaskDepender
(→Custom class definition) |
(→Implementation) |
||
Line 3: | Line 3: | ||
== Introduction == | == Introduction == | ||
− | The <tt>QListWidgetItem</tt> has an attribute: | + | 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"> | + | <syntaxhighlight lang="cpp-qt"> |
− | QVariant QListWidgetItem data (int role ) | + | QVariant QListWidgetItem data(int role) |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 13: | Line 13: | ||
== 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> | </syntaxhighlight> | ||
− | To ensure that the class is compatible with QVariant, the following is added: | + | 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"> | + | <syntaxhighlight lang="cpp-qt"> |
#include <QMetaType> | #include <QMetaType> | ||
Q_DECLARE_METATYPE(MyCustomClass) | Q_DECLARE_METATYPE(MyCustomClass) | ||
Line 44: | 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 56: | 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 61: | 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/> |