Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QItemDelegate::createEditor runs 2 times in first edition



  • Hi all,

    I'm having problems with implementation of a delegate on a QTreeView. Through the delegate I'm creating an editor who works as a toggle so that with a single click the delegate must create the editor, change the status and close the editor.

    It is working properly, but the first time the state is edited, QItemDelegate::createEditor running twice simultaneous, and this makes the toggle remains in the same state for the first time.

    I have refined to meet that is due and only sack clear that occurs and events mousePressed mouseReleased.

    This is the code of the delegate:

    void MyItemDelegate::paint(QPainter *painter,
            const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if ((index.column() == 1))
        {
            QStyleOptionButton buttonOption;
            buttonOption.rect = option.rect;
            buttonOption.state = option.state;
            buttonOption.text = index.model()->data(index).toString();
            buttonOption.palette.setColor(QPalette::ButtonText, buttonOption.text == "Close" ? Qt::red : Qt::darkGreen);
            buttonOption.state |= QStyle::State_None;
    
            QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption,
                    painter);
        }
        else
        {
            QItemDelegate::paint(painter, option, index);
        }
    }
    
    QWidget* MyItemDelegate::createEditor(QWidget *parent,
            const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        if ((index.column() == 1))
        {
            QPushButton* pushButton = new QPushButton(parent);
            //connect(pushButton, SIGNAL(clicked()), this, SLOT(wasClicked()));
            return pushButton;
        }
        else
        {
            return QItemDelegate::createEditor(parent, option, index);
        }
    }
    
    bool MyItemDelegate::editorEvent(QEvent *event,
            QAbstractItemModel *model, const QStyleOptionViewItem &option,
            const QModelIndex &index)
    {
        if (event->type() == QEvent::MouseButtonRelease)
        {
            event->ignore();
            return false;
        }
        else
        {
            return QItemDelegate::editorEvent(event, model, option, index);
        }
    }
    
    bool MyItemDelegate::eventFilter(QObject *editor, QEvent *event)
    {
        bool ret = QItemDelegate::eventFilter(editor, event);
        QPushButton *pushButton = static_cast<QPushButton*>(editor);
        emit commitData(pushButton);
        emit closeEditor(pushButton);
        return ret;
    }
    
    void MyItemDelegate::setEditorData(QWidget *editor,
            const QModelIndex &index) const
    {
        if ((index.column() == 1))
        {
            QVariant value = index.model()->data(index);
            QPushButton* pushButton = static_cast<QPushButton*>(editor);
            pushButton->setText(value.toString());
            QString color = QString::fromStdString(value.toString() == "Close" ? "red": "darkgreen");
            pushButton->setStyleSheet(QString("QPushButton{ color:" + color + ";}"));
            //pushButton->click();
        }
        else
        {
            QItemDelegate::setEditorData(editor, index);
        }
    }
    
    void MyItemDelegate::setModelData(QWidget *editor,
            QAbstractItemModel *model, const QModelIndex &index) const
    {
        if ((index.column() == 1))
        {
            QPushButton* pushButton = static_cast<QPushButton*>(editor);
            pushButton->setText(pushButton->text() == "Close" ? "Open" : "Close");
            QVariant value = pushButton->text() == "Close" ? true : false;
            model->setData(index, value);
        }
        else
        {
            QItemDelegate::setModelData(editor, model, index);
        }
    }
    

    Can anyone explain to me that it is because the first run twice simultaneous?

    Thanks



  • I have the same problem.
    Does anybody have any solution?
    Thank you.


Log in to reply