Qt - use QListWidgetItem user data
From TaskDepender
(→Implementation) |
(→Implementation) |
||
Line 45: | Line 45: | ||
TODO: create actual small application. | TODO: create actual small application. | ||
+ | |||
+ | === Add item === | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
void MainWindow::addItem() | 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 58: | 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 63: | Line 69: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | === Display data === | |
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
Line 73: | Line 79: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | === Change data === | ||
<syntaxhighlight lang="cpp-qt"> | <syntaxhighlight lang="cpp-qt"> | ||
Line 84: | Line 92: | ||
myClass.a += 1; | myClass.a += 1; | ||
− | + | qvPtr->setValue(myClass); | |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |