C++ (Qt)#include <QApplication>#include <QKeyEvent>#include <QPainter>#include <QPushButton>#include <QStandardItemModel>#include <QStyledItemDelegate>#include <QTableView> class ButtonItemDelegate : public QStyledItemDelegate{ Q_OBJECTpublic: ButtonItemDelegate( QObject* p = 0 ) : QStyledItemDelegate( p ) {} protected: void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { QStyleOptionButton opt; opt.rect = option.rect; opt.text = index.data().toString(); if( QWidget* w = dynamic_cast< QWidget* >( painter->device() ) ) { if( w->isEnabled() ) opt.state |= QStyle::State_Enabled; if( w->window()->isActiveWindow() ) opt.state |= QStyle::State_Active; } else { opt.state |= QStyle::State_Enabled; } QApplication::style()->drawControl( QStyle::CE_PushButton, &opt, painter ); } QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const { QPushButton* pb = new QPushButton( index.data().toString(), parent ); pb->setAutoDefault( true ); connect( pb, SIGNAL( clicked() ), this, SLOT( buttonPressed() ) ); return pb; } void setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const { } bool eventFilter( QObject* o, QEvent* e ) { if( e->type() == QEvent::KeyPress ) { switch( static_cast< QKeyEvent* >( e )->key() ) { case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Escape: return false; default: break; } } else if( e->type() == QEvent::FocusOut ) return false; return QStyledItemDelegate::eventFilter( o, e ); }protected Q_SLOTS: void buttonPressed() { qDebug( "Button pressed..." ); }}; int main( int argc, char** argv ){ QApplication app( argc, argv ); QTableView tv; QStandardItemModel m; tv.setModel( &m ); tv.setItemDelegate( new ButtonItemDelegate( &tv ) ); tv.setEditTriggers( QAbstractItemView::CurrentChanged | QAbstractItemView::SelectedClicked ); int rc = 20; m.setColumnCount( rc ); m.setRowCount( rc ); for( int r = 0; r < rc; r++ ) for( int c = 0; c < rc; c++ ) { QModelIndex mi = m.index( r, c ); m.setData( mi, QString( "%1:%2" ). arg( r + 1, 2, 10, QChar( '0' ) ). arg( c + 1, 2, 10, QChar( '0' ) ), Qt::DisplayRole ); } tv.show(); return app.exec();}