QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
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:
Code:
{
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:
Code:
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.
Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
Can you please show where the function is called?
Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
This sample code:
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();
}
Output:
That is, point (0,0) is missing. This happens, presumably, because QVariant::isNull() calls QPoint::isNull() which returns true for QPoint(0,0).
Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
Quote:
Originally Posted by
ChrisW67
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:
Code:
#include <QtGui>
#include <QDebug>
class PointDelegate : public QStyledItemDelegate
{
public:
PointDelegate
(QObject *parent
= 0): QStyledItemDelegate
(parent
) {};
{
if (QVariant::Point == index.
data().
type()) return displayText
(index.
data,
QLocale::system());
else
return QStyledItemDelegate::paint(painter, option, index);
}
{
Q_UNUSED(locale);
qDebug() << "Delegate called with " << value;
return QString("{%1,%2}").
arg(value.
toPoint().
x()).
arg(value.
toPoint().
y());
}
};
Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
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?
Code:
if(test.isNull()){
qDebug() << "Huh?";
}
// = Huh?
Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
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.
Code:
class PointDelegate : public QStyledItemDelegate
{
public:
PointDelegate
(QObject *parent
= 0): QStyledItemDelegate
(parent
) {}
{
if (QVariant::Point == index.
data().
type()) {
painter->drawText(option.rect,
displayText
(index.
data(),
QLocale::system()),
option.displayAlignment);
}
else
QStyledItemDelegate::paint(painter, option, index);
}
{
Q_UNUSED(locale);
qDebug() << "Delegate called with " << value;
return QString("{%1,%2}").
arg(value.
toPoint().
x()).
arg(value.
toPoint().
y());
}
};
Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?
Cool, did work now. Hey thanks for the help!