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-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
− | QVariant QListWidgetItem data (int role ) | + | QVariant QListWidgetItem data(int role) |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 25: | Line 25: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | To ensure that the class is compatible with QVariant, the following is added<ref>[http:// | + | 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"> | <syntaxhighlight lang="cpp-qt"> | ||
Line 43: | Line 43: | ||
== Implementation == | == Implementation == | ||
+ | |||
+ | TODO: create actual small application. | ||
+ | |||
+ | === Add item === | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
− | void MainWindow:: | + | 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-qt"> | ||
− | void MainWindow:: | + | void MainWindow::displayData(QListWidgetItem *item) |
{ | { | ||
− | 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"> | <syntaxhighlight lang="cpp-qt"> | ||
− | void MainWindow:: | + | void MainWindow::changeData(QListWidgetItem *item) |
{ | { | ||
− | QVariant * | + | QVariant *qvPtr = item->data(Qt::UserRole); |
− | MyCustomClass myClass = | + | MyCustomClass myClass = qvPtr->value<MyCustomClass>(); |
− | + | ||
myClass.a += 1; | myClass.a += 1; | ||
− | + | qvPtr->setValue(myClass); | |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | == References == | ||
+ | <references/> |