Qt - use QListWidgetItem user data

From TaskDepender
Jump to: navigation, search
(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 )const [virtual]
+
QVariant QListWidgetItem data(int role)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 13: Line 13:
 
== Custom class definition ==
 
== Custom class definition ==
  
A custom class is defined as follows:
+
Suppose a custom class is defined as follows:
  
<syntaxhighlight lang="cpp">
+
<syntaxhighlight lang="cpp-qt">
 
class MyCustomClass
 
class MyCustomClass
 
{
 
{
 
public:
 
public:
     MyCustomClass() { value_a=0; }
+
     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>
 
Q_DECLARE_METATYPE(MyCustomClass)
 
Q_DECLARE_METATYPE(MyCustomClass)
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 43: Line 44:
 
== Implementation ==
 
== Implementation ==
  
<syntaxhighlight lang="cpp">
+
TODO: create actual small application.
void MainWindow::on_pushButton_clicked()
+
 
 +
=== 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 55: 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 60: Line 69:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
=== Display data ===
  
 
+
<syntaxhighlight lang="cpp-qt">
<syntaxhighlight lang="cpp">
+
void MainWindow::displayData(QListWidgetItem *item)
void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
+
 
{
 
{
     textLineEdit->setText(current->text());
+
     textLineEdit->setText(item->text());
     MyCustomClass myClass = current->data(Qt::UserRole).value<MyCustomClass>();
+
     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/>

Latest revision as of 09:30, 24 December 2011

Personal tools