Selection of row in QItemDelegate, widget not displayed



  • Hi All,

    I have implemented custom delegate. We have used QTableView to display the data from the model.

    Suppose we have inserted 10 rows and 4 columns. In 2 column we have inserted item Delegate as QPushButton. so when we select the (1,4) first row and fourth column then whole row will be highlighted but the QPushButton is not getting displayed. QPushButton is displayed only when we select the second column where we have inserted the QPushButton as item delegate.

    Can you please tell me how can i do this (any row is selected then corresponding item Delegate as QPushbutton shouold be displayed)

    Thanks,
    Neel



  • Well, could you show us the implementation of the delegate? It's hard to say what you did wrong if you are not showing us the code...

    Note that if you want to put widgets in an item view, you should probably use the setIndexWidget() method.



  • I also want behaviour that is QPushButton that we have added in QTableView will be displayed only when MouseOver event will come. How to achieve this ?



  • I have tried with setIndexWidget only but i think it is limitation of qt.
    Thanks for reply ...



  • Below is my delegate implemenation.

    @class CountryDelegate :public QItemDelegate
    {
    Q_OBJECT

    public:
    virtual QWidget* createEditor ( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    {
    QComboBox* editor = new QComboBox( parent );
    editor->installEventFilter( const_cast<CountryDelegate*>(this) );
    return editor;
    }

    virtual void setEditorData( QWidget* editor, const QModelIndex& index ) const
    {
    QComboBox* combo = static_cast<QComboBox*>( editor );
    combo->addItems( countries() );
    int idx = CountryDelegate::countries().indexOf( index.data( Qt::DisplayRole ).toString() );
    combo->setCurrentIndex( idx );
    }

    virtual void setModelData( QWidget * editor, QAbstractItemModel* model, const QModelIndex& index ) const
    {
    QComboBox* combo = static_cast<QComboBox*>( editor );
    model->setData( index, combo->currentText() );
    }

    virtual void updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem& option, const QModelIndex& index ) const
    {

    // Just a silly example, don't allow the editor to get a smaller height than its sizehint.
    int hCell = option.rect.height();
    int hEditor = editor->sizeHint().height();
    int h = qMax( hCell, hEditor );
    QSize size = option.rect.size();
    size.setHeight( h );
    editor->setGeometry( QRect( option.rect.topLeft() - QPoint(0,(h-hCell)/2), size ) );
    }

    virtual bool eventFilter( QObject* obj, QEvent* event )
    {
    if ( event->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Return ) {
    emit commitData( static_cast<QWidget*>(obj) );
    emit closeEditor( static_cast<QWidget*>(obj), EditNextItem );
    }
    return false;
    }

    static QStringList countries()
    {
    QStringList countries;
    countries << "Denmark" << "Sweeden" << "Norway" << "USA" << "Germany"
    << "Poland" << "Iceland" << "Holland" << "Great Britain" << "Ireland" << "Scotland";
    return countries;
    }
    };@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.