4

Given a QTableWidget, is there a way to set an "hidden" value for a cell (QTableWidgetItem), different from the displayed value?

For example, my cell should show "Item 1" text, but double clicking on it the editing should be only on the value 1, showing a spinbox defaulted to 1. In other words, the text showed by the cell should be created started from a value (hidden) associated with the cell.

I cannot find the proper QT function on QTableWidgetItem.

ABCplus
  • 3,981
  • 3
  • 27
  • 43

1 Answers1

4

Yes, you can do that with using QTableWidgetItem::setData() function. The first argument defines the role and second one is the data itself. Besides of the standard roles (Qt::DisplayRole that defines the item text, etc.) you can use your custom roles to store additional data. F

QTableWidgetItem item;
// Store the custom "invisible" data: 22
item.setData(Qt::UserRole, 22);

To retrieve it, you have to use the same role:

QVariant v = item.data(Qt::UserRole);
int i = v.toInt();

In general, for the sake of better code style, you can use enum to define you custom data:

enum {
    MyIntData = Qt::UserRole,
    MyDblData,
    MySuperItem
};

UPDATE

Here is the alternative solution with using item delegate class:

class Delegate : public QItemDelegate
{
public:
    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QVariant value = index.model()->data(index, Qt::UserRole);
        // If the editor is a spin box, set its value.
        QSpinBox *spin = qobject_cast<QSpinBox *>(editor);
        if (spin) {
            spin->setValue(value.toInt());
        } else {
            QItemDelegate::setEditorData(editor, index);
        }
    }
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                        const QModelIndex &index) const
    {
        QSpinBox *spin = qobject_cast<QSpinBox *>(editor);
        if (spin) {
            int value = spin->value();
            // If the value is changed, update the data.
            if (value != index.model()->data(index, Qt::UserRole).toInt()) {
                model->setData(index, value, Qt::DisplayRole);
                model->setData(index, value, Qt::UserRole);
            }
        } else {
            QItemDelegate::setModelData(editor, model, index);
        }
    }
};

And how to create the table widget and item(s):

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QTableWidget tw(1, 1);
    tw.setItemDelegate(new Delegate);

    QTableWidgetItem *item = new QTableWidgetItem();
    item->setData(Qt::UserRole, 22);
    item->setData(Qt::DisplayRole, 33);
    tw.setItem(0, 0, item);
    tw.show();

    [..]
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • I've tried your code, using `setText` for the displayed data and `setData(Qt::UserRole, myData)` to store the "hidden" value. Then I've connected the `cellChanged(int, int)` signal to a slot but double clicking the cell the edit is on the showed label ("Item 1") and not on the real value (1). What am I missing? – ABCplus Jan 19 '15 at 13:38
  • @ABCplus, well, than please try the same by setting your custom data with Qt::EditRole, i.e. `setData(Qt::EditRole, myData)`. – vahancho Jan 19 '15 at 13:50
  • Done, but while `setData(Qt::UserRole, myData)` set `myData` value as hidden, `setData(Qt::EditRole, myData)` shows the value on the cell – ABCplus Jan 19 '15 at 13:53
  • Data set with Qt::EditRole role will be visible when you edit your cell. If cell is not in edit mode, than the value set with the `Qt::DisplayRole` role will be shown. – vahancho Jan 19 '15 at 13:56
  • What I see is that the setData function overwrite the previous, meaning that if I write `setData(Qt::EditRole, 1)` and then `setData(Qt::DisplayRole, 2)` the data showed in the cell is 2 and double clicking on the cell a spinbox with value 2 is showed. Is there some extra property to be set to the `QTableWidget`? – ABCplus Jan 19 '15 at 14:05
  • @ABCplus, please take a look on my undated answer. It is proposed to use the item delegate class that will handle the custom data for cell editors. – vahancho Jan 19 '15 at 14:38