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. QItemDelegate::createEditor runs 2 times in first edition
QtWS25 Last Chance

QItemDelegate::createEditor runs 2 times in first edition

Scheduled Pinned Locked Moved General and Desktop
qitemdelegateqtreeview
2 Posts 2 Posters 1.5k 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.
  • scastielloS Offline
    scastielloS Offline
    scastiello
    wrote on last edited by scastiello
    #1

    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

    C 1 Reply Last reply
    1
    • scastielloS scastiello

      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

      C Offline
      C Offline
      cimer84
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1

      • Login

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