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 );
};
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;
};
To copy to clipboard, switch view to plain text mode
QtCellItemDelegate
::QtCellItemDelegate( QWidget *parent
) : QStyledItemDelegate( parent )
{
qDebug() << "CONSTRUCT"; // this gets printed, good!
}
void QtCellItemDelegate
::paint( QPainter *painter,
{
//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();
}
{
}
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 );
}
To copy to clipboard, switch view to plain text mode
{
Q_OBJECT;
public:
friend class QtFactory;
int Exec();
~QtDirector() {}
protected:
QtDirector( int &argc, char **arg,
std::shared_ptr<QtPuzzleModel> puzzleModel );
private:
QtGameApplication *_app;
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(
_window->show();
}
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();
}
To copy to clipboard, switch view to plain text mode
{
Q_OBJECT;
public:
// typedef std::map<Sudoku::Position, int > CellModelMap;
std::shared_ptr<Sudoku::IPuzzleAccess> access,
std::shared_ptr<Sudoku::GameController> gameController,
std::shared_ptr<Sudoku::PuzzleController> puzzleController,
std::shared_ptr<Sudoku::CellController> cellController );
// read data
// edit data
int role = Qt::EditRole );
Qt
::ItemFlags flags
( const QModelIndex & index
) const;
// disable some stuff
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 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;
};
To copy to clipboard, switch view to plain text mode
{
Q_OBJECT;
public:
QtPuzzleView
( QWidget *parent
= NULL );
protected:
void resizeRows( int rowCount );
void resizeCols( int colCount );
protected slots:
void columnCountChanged( int oldCount, int newCount );
void rowCountChanged( int oldCount, int newCount );
};
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 );
};
To copy to clipboard, switch view to plain text mode
Bookmarks