PDA

View Full Version : QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?



toglia3d
4th September 2010, 04:33
Its basically pretty simple. I need to put QPoints in my QStandardItemModel, and to render them in the QTableView I sub-classed the QStyledItemDelegate and customized the displayText method to:

virtual QString displayText(const QVariant & value, const QLocale & locale ) const
{
Q_UNUSED(locale);
return QString("{%1,%2}").arg(value.toPoint().x()).arg(value.toPoint().y() );

}
So far so good. It worked perfectly after I setted the Delegate for the specific column containing QPoints:

tableView->setItemDelegateForColumn(1, pointDelegate);

Now, for some weird, weird reason the displayText method is not entering when the model has a point with zero on both coordinates. It does nothing to do with the QVariant or parsing the value cause I can confirm its just not entering the method at all.

Why is that? Looks like a bug to me.

tbscope
4th September 2010, 06:18
Can you please show where the function is called?

ChrisW67
4th September 2010, 09:47
This sample code:


#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();
}

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) )
That is, point (0,0) is missing. This happens, presumably, because QVariant::isNull() calls QPoint::isNull() which returns true for QPoint(0,0).

Lykurg
4th September 2010, 10:02
This happens, presumably, because QVariant::isNull() calls QPoint::isNull() which returns true for QPoint(0,0).That's probably the reason. A quick work around is:


#include <QtGui>
#include <QDebug>
class PointDelegate : public QStyledItemDelegate
{
public:
PointDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {};

void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index )
{
if (QVariant::Point == index.data().type())
return displayText(index.data, QLocale::system());
else
return QStyledItemDelegate::paint(painter, option, index);
}

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() );
}
};

toglia3d
4th September 2010, 20:12
I tried the workaround, It's not working for me. Sounds pretty reasonable what ChrisW67 said, in that case based on what grounds can Qt consider an origin point NULL? Should this be declared as a bug?

QPoint test(0,0);
if(test.isNull()){
qDebug() << "Huh?";
}
// = Huh?

Lykurg
4th September 2010, 20:34
Wow, ok that was too early. Three typos, I am scared... But beside that, my first thought was also false. You have to do the drawing yourself.
class PointDelegate : public QStyledItemDelegate
{
public:
PointDelegate(QObject *parent = 0): QStyledItemDelegate(parent)
{}

void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if (QVariant::Point == index.data().type())
{
painter->drawText(option.rect,
displayText(index.data(), QLocale::system()),
option.displayAlignment);
}
else
QStyledItemDelegate::paint(painter, option, index);
}

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() );
}
};

toglia3d
4th September 2010, 22:19
Cool, did work now. Hey thanks for the help!