January 19, 2012

neel2818 neel2818
Lab Rat
57 posts

Delegate display on row selection in QTableView

 

Hi,

I have implemented the custom delegate in QTableView. In One column i have set the ComBox as delegate in QTableView.
Problem is when i select the row of QTableView then ComboBox is not getting displayed. Combobox is only ge displayed when i clicked on second column where i have set the delegate.

  1.         QComboDelegate(QAbstractItemView* view)
  2.         {
  3.             this->view = view;            
  4.         }
  5.  
  6.  
  7.         virtual QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
  8.         {
  9.             Q_UNUSED(index);
  10.             Q_UNUSED(option);
  11.  
  12.             QComboBox* editor = new QComboBox( parent );
  13.  
  14.             editor->installEventFilter( const_cast<QComboDelegate*>(this) );
  15.             editor->setAutoFillBackground(true);
  16.             QObject::connect( editor, SIGNAL(currentIndexChanged(int)), view, SLOT(sensorModeChanged(int)));
  17.  
  18.             return editor;
  19.         }
  20.  
  21.  
  22.         virtual void setEditorData( QWidget* editor, const QModelIndex& index ) const
  23.         {
  24.             QComboBox* combo = static_cast<QComboBox*>( editor );
  25.  
  26.             combo->addItems( comboList() );
  27.             int idx = QComboDelegate::comboList().indexOf( index.data( Qt::DisplayRole ).toString() );
  28.             combo->setCurrentIndex( idx );
  29.         }
  30.  
  31.         virtual void setModelData( QWidget * editor, QAbstractItemModel* model, const QModelIndex& index ) const
  32.         {
  33.             QComboBox* combo = static_cast<QComboBox*>( editor );
  34.  
  35.             model->setData( index, combo->currentText() );
  36.         }
  37.  
  38.         virtual void updateEditorGeometry(QWidget *editor,
  39.                                           const QStyleOptionViewItem& option, const QModelIndex& index ) const
  40.         {
  41.             Q_UNUSED(index);
  42.             int hCell = option.rect.height();
  43.             int hEditor = editor->sizeHint().height();
  44.             int h = qMax( hCell, hEditor );
  45.             QSize size = option.rect.size();
  46.  
  47.             size.setHeight( h );
  48.             editor->setGeometry( QRect( option.rect.topLeft() - QPoint(0, (h - hCell) / 2), size ) );
  49.         }
  50.  
  51.         /**
  52.          * handle the events anf filter the required events
  53.          */
  54.         virtual bool eventFilter( QObject* obj, QEvent* event )
  55.         {
  56.             if ( event->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Return ) {
  57.                 emit commitData( static_cast<QWidget*>(obj) );
  58.                 emit closeEditor( static_cast<QWidget*>(obj), EditNextItem );
  59.             }
  60.  
  61.             return false;
  62.         }

Can you please help me how to do when i select the row then delegate should be displayed ?

Thanks,
Neel

0 replies

 
  ‹‹ How to GET a QDockWidget position ?      GraphicsView doesn’t display Pixmap ››

You must log in to post a reply. Not a member yet? Register here!