Results 1 to 5 of 5

Thread: Reimplement paint() in QItemDelegate

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Reimplement paint() in QItemDelegate

    Hi!

    I have subclassed a QSqlQueryModel and a QItemDelegate. Making a column containing a date editable, and displayed as a QCheckBox. Cell contains a date = checked cell is null = not checked. Checking a cell = insert todays date, unchecking a cell = set date to "".

    The only thing left to do is to allways display the checkbox delegate, even when not editing. After a bit of googling I found out that i have to reimplement the paint() function in my QItemDelegate, to display a CheckBox instead of text.

    The problem is that i have no idea where to begin. The manual gives a short example, but it doesent make much sense to me.

    Does anyone know of a better example?

    The implementation of my QItemDelegate (Basically yanked from the SpinBoxDelegate example):
    Qt Code:
    1. #include "economyhoursattdelegate.h"
    2. #include <QCheckBox>
    3. #include <QDate>
    4.  
    5. /* Constructor
    6.  *
    7.  */
    8. EconomyHoursAttDelegate::EconomyHoursAttDelegate(QObject *parent) :
    9. QItemDelegate(parent)
    10. {
    11. }
    12.  
    13. /* Reimplemented function: createEditor
    14.  * Create a QCheckbox in the att column
    15.  */
    16. QWidget *EconomyHoursAttDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &/*option*/,
    17. const QModelIndex &/*index*/ ) const
    18. {
    19. QCheckBox *editor = new QCheckBox( parent );
    20.  
    21. return editor;
    22. }
    23.  
    24. /* Reimplemented function: setEditorData
    25.  * Set the checkbox checked if cell contains a date
    26.  */
    27. void EconomyHoursAttDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
    28. {
    29. QString value = index.model()->data( index, Qt::EditRole ).toString();
    30.  
    31. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    32.  
    33. bool checked = value == "" ? false : true;
    34.  
    35. checkBox->setChecked( checked );
    36. }
    37.  
    38. /* Reimplemented function: setModelData
    39.  * Set model data to todays date if the checkbox is checked
    40.  */
    41. void EconomyHoursAttDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
    42. const QModelIndex &index ) const
    43. {
    44. QCheckBox *checkBox = static_cast<QCheckBox*>(editor);
    45.  
    46. QDate now = QDate::currentDate();
    47.  
    48. if( checkBox->isChecked() )
    49. model->setData( index, now, Qt::EditRole );
    50. else
    51. model->setData( index, QString(), Qt::EditRole );
    52. }
    53.  
    54. /* Reimplemented function: updateEditorGeometry
    55.  * Adjust the size of the checkbox to fit the cell size
    56.  */
    57. void EconomyHoursAttDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option,
    58. const QModelIndex &/*index*/ ) const
    59. {
    60. editor->setGeometry( option.rect );
    61. }
    To copy to clipboard, switch view to plain text mode 

    Any help is apreciated...

    //John

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reimplement paint() in QItemDelegate

    How about using Qt::CheckStateRole in your model instead?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement paint() in QItemDelegate

    Quote Originally Posted by wysota View Post
    How about using Qt::CheckStateRole in your model instead?
    I am sorry, but i don't follow. Could you elaborate?

    This is the implementation of my model:
    Qt Code:
    1. #include "economyhoursmodel.h"
    2. #include <QSqlQuery>
    3. #include <QDebug>
    4.  
    5. /* Constructor
    6.  *
    7.  */
    8. EconomyHoursModel::EconomyHoursModel( QObject *parent ) :
    9. QSqlQueryModel( parent )
    10. {
    11. }
    12.  
    13. /* Reimplemented function: flags
    14.  * Set the att coulumn editable flag
    15.  */
    16. Qt::ItemFlags EconomyHoursModel::flags( const QModelIndex &index ) const
    17. {
    18. Qt::ItemFlags flags = QSqlQueryModel::flags( index );
    19.  
    20. if( index.column() == 3 )
    21. flags |= Qt::ItemIsEditable;
    22.  
    23. return flags;
    24. }
    25.  
    26. /* Reimplemented function: setData
    27.  * Sets data for the att column
    28.  */
    29. bool EconomyHoursModel::setData( const QModelIndex &index, const QVariant &value, int /*role*/ )
    30. {
    31. if( index.column() != 3 )
    32. return false;
    33.  
    34. QModelIndex idIndex = QSqlQueryModel::index( index.row(), 4 );
    35.  
    36. int id = data( idIndex ).toInt();
    37.  
    38. clear();
    39.  
    40. QSqlQuery query;
    41. query.prepare( "UPDATE hours SET att=? WHERE id=?" );
    42. query.addBindValue( value );
    43. query.addBindValue( id );
    44.  
    45. bool ret = query.exec();
    46.  
    47. if( ret )
    48. refresh();
    49.  
    50. return ret;
    51. }
    52.  
    53. /* Public function: refresh
    54.  * Refresh the query with current start and end date
    55.  */
    56. void EconomyHoursModel::refresh()
    57. {
    58. QSqlQuery query;
    59.  
    60. query.prepare( "SELECT hours.date, hours.hours, repairmen.name, hours.att, hours.id\n"
    61. "FROM hours\n"
    62. "LEFT JOIN repairmen\n"
    63. "ON repairmen.id=hours.repairmen_id\n"
    64. "WHERE hours.date>=? AND hours.date<=?;" );
    65. query.addBindValue( StartDate );
    66. query.addBindValue( EndDate );
    67.  
    68. query.exec();
    69.  
    70. setQuery( query );
    71.  
    72. setHeaderData( 0, Qt::Horizontal, "Datum" );
    73. setHeaderData( 1, Qt::Horizontal, "Antal" );
    74. setHeaderData( 2, Qt::Horizontal, "Reparatör" );
    75. setHeaderData( 3, Qt::Horizontal, "Att" );
    76. setHeaderData( 4, Qt::Horizontal, "ID" );
    77. }
    78.  
    79. /* Public function: setStartDate
    80.  * set start date for the query and refresh
    81.  */
    82. void EconomyHoursModel::setStartDate( QDate date )
    83. {
    84. StartDate = date;
    85. refresh();
    86. }
    87.  
    88. /* Public function: setEndDate
    89.  * set end date for the query and refresh
    90.  */
    91. void EconomyHoursModel::setEndDate( QDate date )
    92. {
    93. EndDate = date;
    94. refresh();
    95. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reimplement paint() in QItemDelegate

    Check state role allows you to store information about each item in regards of whether it is checked or unchecked and display and edit that information using a standard delegate. I'm sure some of Qt model-view examples demonstrate it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    john_erlandsson (19th January 2012)

  6. #5
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement paint() in QItemDelegate

    Solved it!

    Nice solution. I removed my delegate and reimplemented these functions in my model:
    Qt Code:
    1. /* Reimplemented function: flags
    2.  * Set the att coulumn editable flag
    3.  */
    4. Qt::ItemFlags EconomyHoursModel::flags( const QModelIndex &index ) const
    5. {
    6. Qt::ItemFlags flags = QSqlQueryModel::flags( index );
    7.  
    8. if( index.column() == 5 )
    9. flags |= Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
    10.  
    11. return flags;
    12. }
    13.  
    14. /* Reimplemented function: data
    15.  * Have to reimplement this in order to show a checkbox when not editing the table
    16.  */
    17. QVariant EconomyHoursModel::data(const QModelIndex &index, int role) const
    18. {
    19. QVariant ret;
    20.  
    21. if( role == Qt::CheckStateRole && index.column() == 5 )
    22. {
    23. if( QSqlQueryModel::data( index, Qt::DisplayRole ).toString() == "" )
    24. ret = Qt::Unchecked;
    25. else
    26. ret = Qt::Checked;
    27. }
    28. else if( role == Qt::DisplayRole && index.column() == 5 ) //Don't show date as text
    29. {
    30. ret = QVariant();
    31. }
    32. else
    33. {
    34. ret = QSqlQueryModel::data( index, role );
    35. }
    36.  
    37. return ret;
    38. }
    39.  
    40. /* Reimplemented function: setData
    41.  * Sets data for the att column
    42.  */
    43. bool EconomyHoursModel::setData( const QModelIndex &index, const QVariant &value, int role )
    44. {
    45. bool ret = false;
    46.  
    47. if( role == Qt::CheckStateRole )
    48. {
    49. if( index.column() != 5 )
    50. return false;
    51.  
    52. QModelIndex idIndex = QSqlQueryModel::index( index.row(), 0 );
    53.  
    54. int id = data( idIndex ).toInt();
    55.  
    56. QString storeValue = "";
    57.  
    58. if( value.toInt() == Qt::Checked )
    59. storeValue = QDate::currentDate().toString( "yyyy-MM-dd" );
    60.  
    61. clear();
    62.  
    63. QSqlQuery query;
    64. query.prepare( "UPDATE hours SET att=? WHERE id=?" );
    65. query.addBindValue( storeValue );
    66. query.addBindValue( id );
    67.  
    68. ret = query.exec();
    69.  
    70. if( ret )
    71. refresh();
    72. }
    73. else
    74. {
    75. ret = QSqlQueryModel::setData( index, value, role );
    76. }
    77.  
    78. return ret;
    79. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for pointing me in the right direction!!!

    //John

Similar Threads

  1. High cpu usage in custom QItemDelegate::paint
    By rsilva in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2011, 22:52
  2. How to reimplement sizeHint()?
    By Septi in forum Qt Programming
    Replies: 0
    Last Post: 6th July 2010, 13:07
  3. QItemDelegate paint() is not called
    By Cortex in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2010, 20:03
  4. Drawing a widget in QItemDelegate's paint method
    By darkadept in forum Qt Programming
    Replies: 17
    Last Post: 11th August 2009, 05:15
  5. Reimplement of resizeEvent
    By Shawn in forum Qt Programming
    Replies: 20
    Last Post: 27th May 2007, 10:04

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.