This sample code:
#include <QtGui>
#include <QDebug>
class PointDelegate : public QStyledItemDelegate
{
public:
PointDelegate
(QObject *parent
= 0): QStyledItemDelegate
(parent
) {};
{
Q_UNUSED(locale);
qDebug() << "Delegate called with " << value;
return QString("{%1,%2}").
arg(value.
toPoint().
x()).
arg(value.
toPoint().
y());
}
};
int main(int argc, char *argv[])
{
for (int row = 0; row < 2; ++row) {
for (int column = 0; column < 2; ++column) {
item
->setData
(QPoint(row, column
), Qt
::DisplayRole);
model.setItem(row, column, item);
}
}
PointDelegate p;
v.setItemDelegate(&p);
v.setModel(&model);
v.show();
return app.exec();
}
#include <QtGui>
#include <QDebug>
class PointDelegate : public QStyledItemDelegate
{
public:
PointDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {};
virtual QString displayText(const QVariant & value, const QLocale & locale ) const
{
Q_UNUSED(locale);
qDebug() << "Delegate called with " << value;
return QString("{%1,%2}").arg(value.toPoint().x()).arg(value.toPoint().y());
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model(2, 2);
for (int row = 0; row < 2; ++row) {
for (int column = 0; column < 2; ++column) {
QStandardItem *item = new QStandardItem();
item->setData(QPoint(row, column), Qt::DisplayRole);
model.setItem(row, column, item);
}
}
QTableView v;
PointDelegate p;
v.setItemDelegate(&p);
v.setModel(&model);
v.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Output:
Delegate called with QVariant(QPoint, QPoint(0,1) )
Delegate called with QVariant(QPoint, QPoint(1,0) )
Delegate called with QVariant(QPoint, QPoint(1,1) )
Delegate called with QVariant(QPoint, QPoint(0,1) )
Delegate called with QVariant(QPoint, QPoint(1,0) )
Delegate called with QVariant(QPoint, QPoint(1,1) )
To copy to clipboard, switch view to plain text mode
That is, point (0,0) is missing. This happens, presumably, because QVariant::isNull() calls QPoint::isNull() which returns true for QPoint(0,0).
Bookmarks