Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Selection of row in QItemDelegate, widget not displayed
Forum Updated to NodeBB v4.3 + New Features

Selection of row in QItemDelegate, widget not displayed

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neel2818
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        neel2818
        wrote on last edited by
        #3

        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 ?

        1 Reply Last reply
        0
        • N Offline
          N Offline
          neel2818
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • N Offline
            N Offline
            neel2818
            wrote on last edited by
            #5

            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;
            }
            };@

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved