Check Item in QDropdownBox



  • QStandardItem QDropdownBox

    I have a need for a dropdownbox that has a list of items that are check-able.

    I found this on another site but it has not provided a complete solution.

    I have constructed a edit delegate and the dropdownbox delegate appears as expected and lists the items. However, I am unable to set or unset the check attribute by clicking on the item ... or any other keyboard or mouse action for that matter.

    The only difference in my delegate and the example sited is that I am using QVector<QStandardItems*> items - instead of std::vector.

    I have noticed that if I add the selection attribute, I can select the item and the dropbox closes and the desired result occurs for that item only. That is not the action I desire. I wish to check all appropriate items on the list then close the dropdown and evaluate the list.

    What am I missing?

    Update:

    I have decided to include my unfinished code for discussion. I know there are better ways to do this but I have to start somewhere.

    editdaysdelegate.hpp

    #include <QtCore/QSize>
    #include <QtCore/QModelIndex>
    #include <QtGui/QStandardItemModel>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QStylePainter>
    #include <QtWidgets/QWidget>
    #include <QtWidgets/QAbstractItemView>
    #include <QtWidgets/QStyleOptionViewItem>
    #include <QtWidgets/QItemDelegate>
    #include <QtWidgets/QComboBox>
    #include <QtWidgets/QCheckBox>
    
    
    #define Sun_On 1;
    #define Mon_On 2;
    #define Tue_On 4;
    #define Wed_On 8;
    #define Thu_On 16;
    #define Fri_On 32;
    #define Sat_On 64;
    
    #define Sun_Off 63;
    #define Mon_Off 95;
    #define Tue_Off 111;
    #define Wed_Off 119;
    #define Thu_Off 123;
    #define Fri_Off 125;
    #define Sat_Off 126;
    
    class CheckBoxListDelegate : public QItemDelegate
    {
        Q_OBJECT
    
    public:
        CheckBoxListDelegate(QObject *parent);
        virtual ~CheckBoxListDelegate();
    
        QWidget *createEditor( QWidget *parent,
                               const QStyleOptionViewItem &option,
                               const QModelIndex &index
                             ) const Q_DECL_OVERRIDE;
    
        void setEditorData(QWidget *editor,const QModelIndex &index) const Q_DECL_OVERRIDE;
        void setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;
        void setCurrentValue(const quint8 val);
    
    private slots:
        void slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight);
    
    private:
        quint8 m_value;  // the current bit matrix value from the table
    
        QVector<QStandardItem *> items;
    
        QComboBox *combo;
    
        QStandardItemModel* model;
    
        QStandardItem* item1;
        QStandardItem* item2;
        QStandardItem* item3;
        QStandardItem* item4;
        QStandardItem* item5;
        QStandardItem* item6;
        QStandardItem* item7;
    };
    

    editdaysdelegate.cpp

    #include <QtCore/QDebug>
    #include <QtCore/QVector>
    #include <QtWidgets/QStyle>
    #include <QtWidgets/QStyleOption>
    #include <QtGui/QStandardItem>
    #include <QtGui/QStandardItemModel>
    #include <QtGui/QPainter>
    
    #include "editdaysdelegate.hpp"
    
    //==== Delegate implementation  ====//
    CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent) : QItemDelegate(parent)
    {
        m_value = 0;
    
        combo = new QComboBox;  // the widget to be delegated
    
        model = new QStandardItemModel;  // model for the delegate
    
        item1 = new QStandardItem;
        item1->setText("Sun");
        item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item1->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(0, item1);
        items.append(item1);
    
        item2 = new QStandardItem;
        item2->setText("Mon");
        item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item2->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(1, item2);
        items.append(item2);
    
        item3 = new QStandardItem;
        item3->setText("Tue");
        item3->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item3->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(2, item3);
        items.append(item3);
    
        item4 = new QStandardItem;
        item4->setText("Wed");
        item4->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item4->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(3, item4);
        items.append(item4);
    
        item5 = new QStandardItem;
        item5->setText("Thu");
        item5->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item5->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(4, item5);
        items.append(item5);
    
        item6 = new QStandardItem;
        item6->setText("Fri");
        item6->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item6->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(5, item6);
        items.append(item6);
    
        item7 = new QStandardItem;
        item7->setText("Sat");
        item7->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        item7->setData(Qt::Unchecked, Qt::CheckStateRole);
        model->insertRow(6, item7);
        items.append(item7);
    
        connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(slot_changed(const QModelIndex &, const QModelIndex &)));
    
        combo->setModel(model);
    }
    
    CheckBoxListDelegate::~CheckBoxListDelegate()
    {
        delete item1;
        delete item2;
        delete item3;
        delete item4;
        delete item5;
        delete item6;
        delete item7;
        delete model;
    }
    
    
    void CheckBoxListDelegate::setCurrentValue(const quint8 val)
    {
        m_value = val;
    }
    
    void CheckBoxListDelegate::slot_changed(const QModelIndex &topLeft, const QModelIndex &bottomRight)
    {
        Q_UNUSED(bottomRight)
        quint16 row;
    
        row = topLeft.row();
        QStandardItem *item = items[row];
        if ( item->checkState() == Qt::Unchecked )
        {
            switch ( item->row() )
            {
            case 0:
                m_value = m_value & Sun_Off;
                break;
            case 1:
                m_value = m_value & Mon_Off;
                break;
            case 2:
                m_value = m_value & Tue_Off;
                break;
            case 3:
                m_value = m_value & Wed_Off;
                break;
            case 4:
                m_value = m_value & Thu_Off;
                break;
            case 5:
                m_value = m_value & Fri_Off;
                break;
            case 6:
                m_value = m_value & Sat_Off;
                break;
            }
        }
        else if ( item->checkState() == Qt::Checked )
        {
            switch ( item->row() )
            {
            case 0:
                m_value = m_value |= Sun_On;
                break;
            case 1:
                m_value = m_value |= Mon_On;
                break;
            case 2:
                m_value = m_value |= Tue_On;
                break;
            case 3:
                m_value = m_value |= Wed_On;
                break;
            case 4:
                m_value = m_value |= Thu_On;
                break;
            case 5:
                m_value = m_value |= Fri_On;
                break;
            case 6:
                m_value = m_value |= Sat_On;
                break;
            }
        }
    }
    
    QWidget* CheckBoxListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        Q_UNUSED(option)
        Q_UNUSED(index)
    
        // create check box as our editor
        QComboBox *editor = new QComboBox(parent);
        editor->setModel(model);
        return editor;
    }
    
    void CheckBoxListDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
    {
         //set editor data
         QComboBox *myEditor = static_cast<QComboBox*>(editor);
         myEditor->itemDelegate()->setModelData(editor,model,index);
    }
    
    void CheckBoxListDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const
    {
        //get the value from the editor (CheckBox)
        QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
        bool value = myEditor->isChecked();
    
        //set model data
        QMap<int,QVariant> data;
        data.insert(Qt::DisplayRole,myEditor->text());
        data.insert(Qt::UserRole,value);
        model->setItemData(index,data);
    }
    

    The code here does present a dropdown box in the appropriate column / cell of a table view with the items listed properly. However, the items do not present as QCheckbox items -- merely text item. The also do not allow a selection of any kind. I have missed something somewhere and I am not sure what.

    [edit: fixed coding tags, three ` SGaist]



  • After no reply on this forum I posted to qtcenter and now have a good idea of where the problem lies. See the qtcentre posts for complete story.


  • Lifetime Qt Champion

    Hi,

    Can you share the link please ? So other users may find it more easily :)




  • Lifetime Qt Champion

    Thanks !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.