PDA

View Full Version : Custom QStyledItemDelegate paint function never called



mattsnowboard
1st May 2011, 00:21
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!


class QtCellItemDelegate : public QStyledItemDelegate
{
Q_OBJECT;
public:
QtCellItemDelegate( QWidget *parent = NULL );

void paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index );

QSize sizeHint( const QStyleOptionViewItem &option,
const QModelIndex & index ) const;
};


QtCellItemDelegate::QtCellItemDelegate( QWidget *parent ) :
QStyledItemDelegate( parent )
{
qDebug() << "CONSTRUCT"; // this gets printed, good!
}

void QtCellItemDelegate::paint( QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index )
{
//painter->save();
qDebug() << "Begin Paint"; // this is never printed, function never called
QStyledItemDelegate::paint( painter, option, index );

qDebug() << " Begin custom paint";
// custom stuff...
qDebug() << "End paint";
//painter->restore();
}

QSize QtCellItemDelegate::sizeHint( const QStyleOptionViewItem &option,
const QModelIndex & index ) const
{
return QSize( 60, 60 );
}



class QtDirector : public QObject
{
Q_OBJECT;
public:
friend class QtFactory;

int Exec();

~QtDirector() {}

protected:

QtDirector( int &argc, char **arg,
std::shared_ptr<QtPuzzleModel> puzzleModel );

private:
QtGameApplication *_app;

QTableView *_tableView;

QtMainWindow *_window;

std::shared_ptr<QtPuzzleModel> _model;
std::shared_ptr<QtCellItemDelegate> _delegate;
};

QtDirector::QtDirector( int &argc, char **argv,
std::shared_ptr<QtPuzzleModel> puzzleModel )
: _app( new QtGameApplication( argc, argv ) ),
_tableView( new QtPuzzleView ),
_window( new QtMainWindow( _tableView ) ),
_model( puzzleModel ),
_delegate( new QtCellItemDelegate( _tableView ) )
{
if ( !_model )
{
throw std::runtime_error(
"Cannot instantiate with NULL QtPuzzleModel" );
}
_tableView->setModel( _model.get() );
_tableView->setItemDelegate( _delegate.get() );

qDebug() << "My delegate is: " << _delegate.get(); // output of this
qDebug() << "Table view is using: " << _tableView->itemDelegate(); // and this is the same

connect( _window->GetLoadAction(), SIGNAL( triggered() ),
_model.get(), SLOT( LoadPuzzle() ) );
connect( _window->GetNewAction(), SIGNAL( triggered() ),
_model.get(), SLOT( NewPuzzle() ) );
connect( _window->GetUndoAction(), SIGNAL( triggered() ),
_model.get(), SLOT( Undo() ) );
connect( _window->GetRedoAction(), SIGNAL( triggered() ),
_model.get(), SLOT( Redo() ) );
connect( _window->GetMarkHintsAction(), SIGNAL( triggered() ),
_model.get(), SLOT( MarkPuzzleHints() ) );

connect( _model.get(), SIGNAL( hasPuzzle(bool) ),
_window->GetMarkHintsAction(), SLOT( setEnabled( bool ) ) );

connect( this, SIGNAL( canUndo( bool ) ),
_window->GetUndoAction(), SLOT( setEnabled( bool ) ) );

connect( this, SIGNAL( canRedo( bool ) ),
_window->GetRedoAction(), SLOT( setEnabled( bool ) ) );

_window->setWindowTitle(
QApplication::translate("windowlayout", "Sudoku"));
_window->show();
}


class QtPuzzleModel : public QAbstractTableModel, public Sudoku::ICellObserver
{
Q_OBJECT;
public:
// typedef std::map<Sudoku::Position, int > CellModelMap;

QtPuzzleModel( QObject *parent,
std::shared_ptr<Sudoku::IPuzzleAccess> access,
std::shared_ptr<Sudoku::GameController> gameController,
std::shared_ptr<Sudoku::PuzzleController> puzzleController,
std::shared_ptr<Sudoku::CellController> cellController );
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
// read data
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
// edit data
bool setData( const QModelIndex & index,
const QVariant & value,
int role = Qt::EditRole );
Qt::ItemFlags flags( const QModelIndex & index ) const;

// disable some stuff
QVariant headerData( int section,
Qt::Orientation orientation,
int role ) const;

void Update( const Sudoku::Cell &c );

public slots:
void NewPuzzle();
bool LoadPuzzle();
void MarkPuzzleHints();
void Undo();
void Redo();

signals:
void ValueChanged( int x, int y, int value );
void hasPuzzle( bool yes );

private:
std::shared_ptr<Sudoku::IPuzzleAccess> _access;
std::shared_ptr<Sudoku::GameController> _gameController;
std::shared_ptr<Sudoku::PuzzleController> _puzzleController;
std::shared_ptr<Sudoku::CellController> _cellController;
};


class QtPuzzleView : public QTableView
{
Q_OBJECT;
public:
QtPuzzleView( QWidget *parent = NULL );

virtual void setModel( QAbstractItemModel *model );

protected:
void resizeRows( int rowCount );
void resizeCols( int colCount );

protected slots:
void columnCountChanged( int oldCount, int newCount );
void rowCountChanged( int oldCount, int newCount );
};

wysota
4th May 2011, 20:40
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.

d_stranz
6th May 2011, 02:57
That's not the problem. QStyledItemDelegate:: paint() is a virtual const method. His is not, so the signatures don't match.