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. where to emit closeEditor() when subclass from QItemDelegate ?
Forum Updated to NodeBB v4.3 + New Features

where to emit closeEditor() when subclass from QItemDelegate ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 868 Views
  • 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.
  • Q Offline
    Q Offline
    QtTester
    wrote on last edited by
    #1

    Hi guys.
    I have a tableview , one column need to set to combobox, so i subclass QItemDelegate. it works, except it cannot emit closeEditor() signal, I invoke emit closeEditor in setModelData(), but compiler says:

    error: C2662: “void QAbstractItemDelegate::closeEditor(QWidget *,QAbstractItemDelegate::EndEditHint)”: 不能将“this”指针从“const ComboboxDelegate”转换为“QAbstractItemDelegate &”

    here is the code:

    class ComboboxDelegate: public QItemDelegate
    {
    
    public:
        ComboboxDelegate(QWidget *parent = nullptr,QStringList items=QStringList(),int currentIndex=0):QItemDelegate(parent)
        {
            m_items = items;
            m_initIdx = currentIndex;
        }
    
        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                const QModelIndex &index) const override //final
        {
            Q_UNUSED(parent)
            Q_UNUSED(option)
            Q_UNUSED(index)
    
            QComboBox *channelBox = new QComboBox(parent); 
            channelBox->setFixedHeight(option.rect.height());
    
            for(int i=0;i<m_items.size();i++){
                QString item = m_items.at(i);
                channelBox->addItem(item);
            }
            channelBox->setCurrentIndex(m_initIdx);
            return channelBox;
        }
    
        void setEditorData(QWidget *editor, const QModelIndex &index) const
        {
            QComboBox *comboBox = static_cast<QComboBox*>(editor);
            if(comboBox){
                QString str = index.model()->data(index, Qt::EditRole).toString();
                comboBox->setCurrentIndex(comboBox->findText(str));
            }
        }
    
        void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
        {
            QComboBox *comboBox = static_cast<QComboBox*>(editor);
            if(comboBox!=0){
                model->setData(index, comboBox->currentText(), Qt::EditRole);
                emit closeEditor(editor); // ERROR HERE ~~~~
            }
        }
    
        void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
        {
            editor->setGeometry(option.rect);
        }
        // still cannot work with my signal ~~~
    signals:
        void closeEditor2(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint);
    private:
        int m_initIdx;
        QStringList m_items;
    };
    

    Any help will be appreciated.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You don't have to emit this signal, it's emitted by QAbstractItemDelegate to notify the view.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Q 1 Reply Last reply
      2
      • Q QtTester

        Hi guys.
        I have a tableview , one column need to set to combobox, so i subclass QItemDelegate. it works, except it cannot emit closeEditor() signal, I invoke emit closeEditor in setModelData(), but compiler says:

        error: C2662: “void QAbstractItemDelegate::closeEditor(QWidget *,QAbstractItemDelegate::EndEditHint)”: 不能将“this”指针从“const ComboboxDelegate”转换为“QAbstractItemDelegate &”

        here is the code:

        class ComboboxDelegate: public QItemDelegate
        {
        
        public:
            ComboboxDelegate(QWidget *parent = nullptr,QStringList items=QStringList(),int currentIndex=0):QItemDelegate(parent)
            {
                m_items = items;
                m_initIdx = currentIndex;
            }
        
            QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const override //final
            {
                Q_UNUSED(parent)
                Q_UNUSED(option)
                Q_UNUSED(index)
        
                QComboBox *channelBox = new QComboBox(parent); 
                channelBox->setFixedHeight(option.rect.height());
        
                for(int i=0;i<m_items.size();i++){
                    QString item = m_items.at(i);
                    channelBox->addItem(item);
                }
                channelBox->setCurrentIndex(m_initIdx);
                return channelBox;
            }
        
            void setEditorData(QWidget *editor, const QModelIndex &index) const
            {
                QComboBox *comboBox = static_cast<QComboBox*>(editor);
                if(comboBox){
                    QString str = index.model()->data(index, Qt::EditRole).toString();
                    comboBox->setCurrentIndex(comboBox->findText(str));
                }
            }
        
            void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
            {
                QComboBox *comboBox = static_cast<QComboBox*>(editor);
                if(comboBox!=0){
                    model->setData(index, comboBox->currentText(), Qt::EditRole);
                    emit closeEditor(editor); // ERROR HERE ~~~~
                }
            }
        
            void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
            {
                editor->setGeometry(option.rect);
            }
            // still cannot work with my signal ~~~
        signals:
            void closeEditor2(QWidget *editor, QAbstractItemDelegate::EndEditHint hint = NoHint);
        private:
            int m_initIdx;
            QStringList m_items;
        };
        

        Any help will be appreciated.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @QtTester
        As @Christian-Ehrlicher has said. Your code will be fine without any emits here.

        But (quite separate issue) you might want to change your static_cast<QComboBox*>s to qobject_cast<QComboBox*>s. It's safer, and allows you to use the delegate against different columns/widget types without having to invent separate delegates for each one. Depending on how you like to do these things.

        1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          You don't have to emit this signal, it's emitted by QAbstractItemDelegate to notify the view.

          Q Offline
          Q Offline
          QtTester
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in where to emit closeEditor() when subclass from QItemDelegate ?:

          You don't have to emit this signal, it's emitted by QAbstractItemDelegate to notify the view.

          @JonB said in where to emit closeEditor() when subclass from QItemDelegate ?:

          @QtTester
          As @Christian-Ehrlicher has said. Your code will be fine without any emits here.

          But (quite separate issue) you might want to change your static_cast<QComboBox*>s to qobject_cast<QComboBox*>s. It's safer, and allows you to use the delegate against different columns/widget types without having to invent separate delegates for each one. Depending on how you like to do these things.

          Thanks you both. But I didnot recevie the signal, after I connect the abstractitemdelegate signal, when I finish edit the default lineedit cell ,it emits a signal ,but after I choose the combobox , the signal did not send out!

          connect(ui->tableViewSpdLimit->itemDelegate(),&QAbstractItemDelegate::closeEditor,this,[=](){
                  qDebug("detect edit!");
                  ui->pushButtonSubmitLimit->setEnabled(true);
              });
          
          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            QtTester
            wrote on last edited by
            #5

            Already solved, use ui->tableViewSpdLimit->itemDelegateForColumn()

            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