Qt - use QListWidgetItem user data

From TaskDepender
Jump to: navigation, search
(Created page with "''This page describes how the user data of a QListWidgetItem can be used to store a custom class.'' == Custom class definition == Suppose a custome class is defined as follows:...")
 
(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 custome class is defined as follows:
+
Suppose a custom class is defined as follows:
  
<syntaxhightlight lang="cpp">
+
<syntaxhighlight lang="cpp-qt">
 
class MyCustomClass
 
class MyCustomClass
 
{
 
{
 
public:
 
public:
     MyCustomClass() { a=0; b=0; }
+
     MyCustomClass();
     ~MyCustomClass() {}
+
     ~MyCustomClass();
 
     int a;
 
     int a;
    int b;
 
 
};
 
};
 
</syntaxhighlight>
 
</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>
 +
 +
== GUI ==
 +
 +
A main window with a list and two line edit fields and a push-button.
 +
 +
== Adding the QListWidget ==
 +
 +
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.
 +
 +
== Implementation ==
 +
 +
TODO: create actual small application.
 +
 +
=== Add item ===
 +
 +
<syntaxhighlight lang="cpp-qt">
 +
void MainWindow::addItem()
 +
{
 +
    // Local temporary variables.
 +
    MyCustomClass myClass;
 +
    QVariant      qv;
 +
 +
    myClass.a = ui->valueLineEdit->text().toInt();
 +
   
 +
    // Use content of local variable and copy.
 +
    qv.setValue(myClass);
 +
 +
    QListWidgetItem *qlwi = new QListWidgetItem();
 +
    qlwi->setText(ui->textLineEdit->text());
 +
 +
    // Copy the local qvariant and store with list item.
 +
    qlwi->setData(Qt::UserRole,qv);
 +
    ui->listWidget->addItem(qlwi);
 +
}
 +
</syntaxhighlight>
 +
 +
=== Display data ===
 +
 +
<syntaxhighlight lang="cpp-qt">
 +
void MainWindow::displayData(QListWidgetItem *item)
 +
{
 +
    textLineEdit->setText(item->text());
 +
    MyCustomClass myClass = item->data(Qt::UserRole).value<MyCustomClass>();
 +
    ui->lineEdit->setText(QString("%1").arg(myClass.a));
 +
}
 +
</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