Results 1 to 7 of 7

Thread: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?

  1. #1
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs down 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:
    Qt Code:
    1. virtual QString displayText(const QVariant & value, const QLocale & locale ) const
    2. {
    3. Q_UNUSED(locale);
    4. return QString("{%1,%2}").arg(value.toPoint().x()).arg(value.toPoint().y());
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    So far so good. It worked perfectly after I setted the Delegate for the specific column containing QPoints:
    Qt Code:
    1. tableView->setItemDelegateForColumn(1, pointDelegate);
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by toglia3d; 4th September 2010 at 05:08.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?

    Can you please show where the function is called?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?

    This sample code:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3. class PointDelegate : public QStyledItemDelegate
    4. {
    5. public:
    6. PointDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {};
    7.  
    8. virtual QString displayText(const QVariant & value, const QLocale & locale ) const
    9. {
    10. Q_UNUSED(locale);
    11. qDebug() << "Delegate called with " << value;
    12. return QString("{%1,%2}").arg(value.toPoint().x()).arg(value.toPoint().y());
    13. }
    14. };
    15.  
    16. int main(int argc, char *argv[])
    17. {
    18. QApplication app(argc, argv);
    19.  
    20. QStandardItemModel model(2, 2);
    21. for (int row = 0; row < 2; ++row) {
    22. for (int column = 0; column < 2; ++column) {
    23. item->setData(QPoint(row, column), Qt::DisplayRole);
    24. model.setItem(row, column, item);
    25. }
    26. }
    27.  
    28. PointDelegate p;
    29. v.setItemDelegate(&p);
    30. v.setModel(&model);
    31. v.show();
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. Delegate called with QVariant(QPoint, QPoint(0,1) )
    2. Delegate called with QVariant(QPoint, QPoint(1,0) )
    3. Delegate called with QVariant(QPoint, QPoint(1,1) )
    4. Delegate called with QVariant(QPoint, QPoint(0,1) )
    5. Delegate called with QVariant(QPoint, QPoint(1,0) )
    6. 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).

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?

    Quote Originally Posted by ChrisW67 View Post
    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:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3. class PointDelegate : public QStyledItemDelegate
    4. {
    5. public:
    6. PointDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {};
    7.  
    8. void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index )
    9. {
    10. if (QVariant::Point == index.data().type())
    11. return displayText(index.data, QLocale::system());
    12. else
    13. return QStyledItemDelegate::paint(painter, option, index);
    14. }
    15.  
    16. virtual QString displayText(const QVariant & value, const QLocale & locale ) const
    17. {
    18. Q_UNUSED(locale);
    19. qDebug() << "Delegate called with " << value;
    20. return QString("{%1,%2}").arg(value.toPoint().x()).arg(value.toPoint().y());
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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?
    Qt Code:
    1. QPoint test(0,0);
    2. if(test.isNull()){
    3. qDebug() << "Huh?";
    4. }
    5. // = Huh?
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.
    Qt Code:
    1. class PointDelegate : public QStyledItemDelegate
    2. {
    3. public:
    4. PointDelegate(QObject *parent = 0): QStyledItemDelegate(parent)
    5. {}
    6.  
    7. void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    8. {
    9. if (QVariant::Point == index.data().type())
    10. {
    11. painter->drawText(option.rect,
    12. displayText(index.data(), QLocale::system()),
    13. option.displayAlignment);
    14. }
    15. else
    16. QStyledItemDelegate::paint(painter, option, index);
    17. }
    18.  
    19. QString displayText(const QVariant & value, const QLocale & locale ) const
    20. {
    21. Q_UNUSED(locale);
    22. qDebug() << "Delegate called with " << value;
    23. return QString("{%1,%2}").arg(value.toPoint().x()).arg(value.toPoint().y());
    24. }
    25. };
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPoint(0,0) is not rendered with QStyledItemDelegate, bug or feature?

    Cool, did work now. Hey thanks for the help!

Similar Threads

  1. SVG rendered to an 8 bits indexed QImage ?
    By lauranger in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 21:44
  2. changing svg graphic attributes once rendered
    By barrygp in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2008, 02:09
  3. Programmatically Save Image rendered in a QWebPage
    By last2kn0 in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2008, 06:33
  4. Webkit rendered fonts
    By carl in forum Qt Programming
    Replies: 0
    Last Post: 5th September 2008, 16:49
  5. QPoint Limitation
    By archanasubodh in forum Qt Programming
    Replies: 1
    Last Post: 5th August 2008, 10:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.