Results 1 to 3 of 3

Thread: Custom QStyledItemDelegate paint function never called

  1. #1
    Join Date
    Apr 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Custom QStyledItemDelegate paint function never called

    I have a custom Model, View and Delegate. I am unable to get the Delegate paint function to ever get called. It should print a debug statement, however it behaves as if I never created the delegate Here is a brief part of the code. Please let me know if there are other functions that would be useful to post. Thanks!

    Qt Code:
    1. class QtCellItemDelegate : public QStyledItemDelegate
    2. {
    3. Q_OBJECT;
    4. public:
    5. QtCellItemDelegate( QWidget *parent = NULL );
    6.  
    7. void paint( QPainter *painter,
    8. const QStyleOptionViewItem &option,
    9. const QModelIndex &index );
    10.  
    11. QSize sizeHint( const QStyleOptionViewItem &option,
    12. const QModelIndex & index ) const;
    13. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QtCellItemDelegate::QtCellItemDelegate( QWidget *parent ) :
    2. QStyledItemDelegate( parent )
    3. {
    4. qDebug() << "CONSTRUCT"; // this gets printed, good!
    5. }
    6.  
    7. void QtCellItemDelegate::paint( QPainter *painter,
    8. const QStyleOptionViewItem &option,
    9. const QModelIndex &index )
    10. {
    11. //painter->save();
    12. qDebug() << "Begin Paint"; // this is never printed, function never called
    13. QStyledItemDelegate::paint( painter, option, index );
    14.  
    15. qDebug() << " Begin custom paint";
    16. // custom stuff...
    17. qDebug() << "End paint";
    18. //painter->restore();
    19. }
    20.  
    21. QSize QtCellItemDelegate::sizeHint( const QStyleOptionViewItem &option,
    22. const QModelIndex & index ) const
    23. {
    24. return QSize( 60, 60 );
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class QtDirector : public QObject
    2. {
    3. Q_OBJECT;
    4. public:
    5. friend class QtFactory;
    6.  
    7. int Exec();
    8.  
    9. ~QtDirector() {}
    10.  
    11. protected:
    12.  
    13. QtDirector( int &argc, char **arg,
    14. std::shared_ptr<QtPuzzleModel> puzzleModel );
    15.  
    16. private:
    17. QtGameApplication *_app;
    18.  
    19. QTableView *_tableView;
    20.  
    21. QtMainWindow *_window;
    22.  
    23. std::shared_ptr<QtPuzzleModel> _model;
    24. std::shared_ptr<QtCellItemDelegate> _delegate;
    25. };
    26.  
    27. QtDirector::QtDirector( int &argc, char **argv,
    28. std::shared_ptr<QtPuzzleModel> puzzleModel )
    29. : _app( new QtGameApplication( argc, argv ) ),
    30. _tableView( new QtPuzzleView ),
    31. _window( new QtMainWindow( _tableView ) ),
    32. _model( puzzleModel ),
    33. _delegate( new QtCellItemDelegate( _tableView ) )
    34. {
    35. if ( !_model )
    36. {
    37. throw std::runtime_error(
    38. "Cannot instantiate with NULL QtPuzzleModel" );
    39. }
    40. _tableView->setModel( _model.get() );
    41. _tableView->setItemDelegate( _delegate.get() );
    42.  
    43. qDebug() << "My delegate is: " << _delegate.get(); // output of this
    44. qDebug() << "Table view is using: " << _tableView->itemDelegate(); // and this is the same
    45.  
    46. connect( _window->GetLoadAction(), SIGNAL( triggered() ),
    47. _model.get(), SLOT( LoadPuzzle() ) );
    48. connect( _window->GetNewAction(), SIGNAL( triggered() ),
    49. _model.get(), SLOT( NewPuzzle() ) );
    50. connect( _window->GetUndoAction(), SIGNAL( triggered() ),
    51. _model.get(), SLOT( Undo() ) );
    52. connect( _window->GetRedoAction(), SIGNAL( triggered() ),
    53. _model.get(), SLOT( Redo() ) );
    54. connect( _window->GetMarkHintsAction(), SIGNAL( triggered() ),
    55. _model.get(), SLOT( MarkPuzzleHints() ) );
    56.  
    57. connect( _model.get(), SIGNAL( hasPuzzle(bool) ),
    58. _window->GetMarkHintsAction(), SLOT( setEnabled( bool ) ) );
    59.  
    60. connect( this, SIGNAL( canUndo( bool ) ),
    61. _window->GetUndoAction(), SLOT( setEnabled( bool ) ) );
    62.  
    63. connect( this, SIGNAL( canRedo( bool ) ),
    64. _window->GetRedoAction(), SLOT( setEnabled( bool ) ) );
    65.  
    66. _window->setWindowTitle(
    67. QApplication::translate("windowlayout", "Sudoku"));
    68. _window->show();
    69. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class QtPuzzleModel : public QAbstractTableModel, public Sudoku::ICellObserver
    2. {
    3. Q_OBJECT;
    4. public:
    5. // typedef std::map<Sudoku::Position, int > CellModelMap;
    6.  
    7. QtPuzzleModel( QObject *parent,
    8. std::shared_ptr<Sudoku::IPuzzleAccess> access,
    9. std::shared_ptr<Sudoku::GameController> gameController,
    10. std::shared_ptr<Sudoku::PuzzleController> puzzleController,
    11. std::shared_ptr<Sudoku::CellController> cellController );
    12. int rowCount( const QModelIndex &parent = QModelIndex() ) const;
    13. int columnCount( const QModelIndex &parent = QModelIndex() ) const;
    14. // read data
    15. QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
    16. // edit data
    17. bool setData( const QModelIndex & index,
    18. const QVariant & value,
    19. int role = Qt::EditRole );
    20. Qt::ItemFlags flags( const QModelIndex & index ) const;
    21.  
    22. // disable some stuff
    23. QVariant headerData( int section,
    24. Qt::Orientation orientation,
    25. int role ) const;
    26.  
    27. void Update( const Sudoku::Cell &c );
    28.  
    29. public slots:
    30. void NewPuzzle();
    31. bool LoadPuzzle();
    32. void MarkPuzzleHints();
    33. void Undo();
    34. void Redo();
    35.  
    36. signals:
    37. void ValueChanged( int x, int y, int value );
    38. void hasPuzzle( bool yes );
    39.  
    40. private:
    41. std::shared_ptr<Sudoku::IPuzzleAccess> _access;
    42. std::shared_ptr<Sudoku::GameController> _gameController;
    43. std::shared_ptr<Sudoku::PuzzleController> _puzzleController;
    44. std::shared_ptr<Sudoku::CellController> _cellController;
    45. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class QtPuzzleView : public QTableView
    2. {
    3. Q_OBJECT;
    4. public:
    5. QtPuzzleView( QWidget *parent = NULL );
    6.  
    7. virtual void setModel( QAbstractItemModel *model );
    8.  
    9. protected:
    10. void resizeRows( int rowCount );
    11. void resizeCols( int colCount );
    12.  
    13. protected slots:
    14. void columnCountChanged( int oldCount, int newCount );
    15. void rowCountChanged( int oldCount, int newCount );
    16. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by mattsnowboard; 30th April 2011 at 23:24. Reason: changed [qtclass] to [code]

  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: Custom QStyledItemDelegate paint function never called

    Make sure the delegate doesn't get destroyed before it is ever used. And make sure you have some data in the model, otherwise there is nothing to paint.
    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 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom QStyledItemDelegate paint function never called

    That's not the problem. QStyledItemDelegate:: paint() is a virtual const method. His is not, so the signatures don't match.

Similar Threads

  1. Paint selection in QStyledItemDelegate's subclass
    By woodtluk in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2012, 15:51
  2. Custom QStyledItemDelegate
    By Berryblue031 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 10:32
  3. QStyledItemDelegate with custom QWidget editor
    By stefanadelbert in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 23:19
  4. QItemDelegate paint() is not called
    By Cortex in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2010, 20:03
  5. update custom QStyledItemDelegate after drag
    By dano in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 18:29

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.