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. Custom QItemDelegate works in QTableView but not QTreeView
Forum Updated to NodeBB v4.3 + New Features

Custom QItemDelegate works in QTableView but not QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
qitemdelegateitem delegate
8 Posts 2 Posters 5.8k Views 2 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by Joel Bodenmann
    #1

    I wrote a custom QItemDelegate to put a QCheckBox in a column of a QTableView which worked quite well:

    #include <QCheckBox>
    #include <QHBoxLayout>
    #include "toolwidget/checkboxdelegate.h"
    
    CheckboxDelegate::CheckboxDelegate(QObject* parent) : QItemDelegate(parent)
    {
    }
    
    QWidget* CheckboxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        Q_UNUSED(option)
        Q_UNUSED(index)
    
        // Create a new checkbox. Use a layout to align (center) it.
        QCheckBox* editor = new QCheckBox(parent);
        editor->setObjectName("CheckBox");
        QHBoxLayout* layout = new QHBoxLayout;
        layout->addStretch();
        layout->addWidget(editor);
        layout->addStretch();
        QWidget* editorWidget = new QWidget(parent);
        editorWidget->setLayout(layout);
    
        return editorWidget;
    }
    
    void CheckboxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    {
        // Retrieve the checked state from the item
        bool checked = index.model()->data(index, Qt::EditRole).toBool();
    
        // Retrieve the checkbox
        QCheckBox* checkbox = static_cast<QCheckBox*>(editor->findChild<QCheckBox*>("CheckBox"));
        if (!checkbox) {
            qCritical("CheckboxDelegate::setEditorData(): Couldn't retrieve QCheckBox. Aborting.");
            return;
        }
    
        // Set the checkbox state
        checkbox->setChecked(checked);
    }
    
    void CheckboxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
    {
        QCheckBox* checkbox = static_cast<QCheckBox*>(editor->findChild<QCheckBox*>("CheckBox"));
        if (!checkbox) {
            qCritical("CheckboxDelegate::setModelData(): Couldn't retrieve QCheckBox. Aborting.");
            return;
        }
    
        // Set the model data
        model->setData(index, checkbox->isChecked(), Qt::EditRole);
    }
    
    void CheckboxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        Q_UNUSED(index)
    
        editor->setGeometry(option.rect);
    }
    
    void CheckboxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        // Retrieve the checked state from the item
        Qt::CheckState checkState = index.model()->data(index, Qt::DisplayRole).toBool() ? Qt::Checked : Qt::Unchecked;
    
        // Render
        drawCheck(painter, option, option.rect, checkState);
    }
    
    QSize CheckboxDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        Q_UNUSED(option)
        Q_UNUSED(index)
    
        QCheckBox checkbox;
    
        return checkbox.sizeHint();
    }
    

    Now I decided that I'd much rather use a QTreeView than a QTableView because I want to introduce hierarchies in my model. The problem is that my CheckboxDelegate doesn't work there anymore.
    I still see the checkbox being rendered but I can't change the state of it anymore. When I click on it I see that QAbstractItemModel::setData() is being called successfully but it always writes back the same value - this means that the checkbox never gets toggled.

    I'm a bit lost here. I don't know how much more information is required. Any ideas?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Out of curiosity, why not return item flag Qt::ItemIsUserCheckable from your model ? IIRC it should provide you with what you need directly and avoid the need of a custom delegate.

      On a side note, you should use qobject_cast when dealing with QObject casting. static_cast doesn't do any validation.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Joel BodenmannJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Out of curiosity, why not return item flag Qt::ItemIsUserCheckable from your model ? IIRC it should provide you with what you need directly and avoid the need of a custom delegate.

        On a side note, you should use qobject_cast when dealing with QObject casting. static_cast doesn't do any validation.

        Joel BodenmannJ Offline
        Joel BodenmannJ Offline
        Joel Bodenmann
        wrote on last edited by
        #3

        I tried using Qt::ItemIsUserCheckable and it has the expected effect. However, instead of displaying a regular checkbox it displayed a combobox with the values "True" and "False". Hence the custom delegate.

        I replaced the static_cast with qobject_cast as per your recommendation. Of course, as expected, this didn't affect the problem that I'm having.

        Any ideas?

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You also need to modify your data function for the corresponding role.

          See the "extending the read only example with roles" part of the model view tutorial here.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Joel BodenmannJ 1 Reply Last reply
          0
          • SGaistS SGaist

            You also need to modify your data function for the corresponding role.

            See the "extending the read only example with roles" part of the model view tutorial here.

            Joel BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on last edited by
            #5

            I also modified my model. Actually, when I was still using the QTableView I followed exactly the example you linked and I got it working. The reason why I created my own custom delegate anyway is because I want one single checkbox that is centered in the cell. The "built-in" feature (as shown in the example you linked) adds a checkbox to the decoration of a cell.

            As mentioned everything works well in a QTableView. It's just that it doesn't work that well in a QTreeView.

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you setup a minimal project that shows the behavior ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Joel BodenmannJ 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you setup a minimal project that shows the behavior ?

                Joel BodenmannJ Offline
                Joel BodenmannJ Offline
                Joel Bodenmann
                wrote on last edited by Joel Bodenmann
                #7

                @SGaist

                Yup, here you go: <link_removed>
                I hope that helps... really lost here.

                I am using Qt 5.6.1 on Windows 10 64-Bit with MinGW 4.9.2.

                Industrial process automation software: https://simulton.com
                Embedded Graphics & GUI library: https://ugfx.io

                Joel BodenmannJ 1 Reply Last reply
                0
                • Joel BodenmannJ Joel Bodenmann

                  @SGaist

                  Yup, here you go: <link_removed>
                  I hope that helps... really lost here.

                  I am using Qt 5.6.1 on Windows 10 64-Bit with MinGW 4.9.2.

                  Joel BodenmannJ Offline
                  Joel BodenmannJ Offline
                  Joel Bodenmann
                  wrote on last edited by Joel Bodenmann
                  #8

                  At the end, @SGaist pointed me to this implementation of a checkbox item delegate which works very well.

                  Industrial process automation software: https://simulton.com
                  Embedded Graphics & GUI library: https://ugfx.io

                  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