3

I'm trying to do something basic : you have a QTreeView. 1st depth are folders only, 2nd depth are files only. I want to have a check box with the checked status next to each item. Files are either checked or unchecked, folders can also be partiallyChecked depending on their files; all in all quite natural I believe.

The way I though I should go was using a QStandardItemModel and populate it with a custom subclass of QStandardItem : DescriptionFileItem. Maybe that was a bad idea, if there's an easier way please enlight me.

I tried using signals and slots so that my signal CheckStateChanged on a file would be connected to a slot UpdateCheckedStateOnChildStateChanged on its containing folder. This required my DescriptionFileItem to inherit from QObject as well (BTW, I was surprised that QStandardItem did not inherit from QObject). I initially hoped this would work seamlessly with the provided base classes but it did not : emitDataChanged() didn't seem to trigger my model's dataChanged() signal...

Using the model's dataChanged signals directly didn't work either: it's call is protected so you can't use it without subclassing (I think that's my next move unless somebody can help me get it right).

At the moment I have a signal -> slot connection that won't work and I have no idea why; compile and link work ok. Here's the code; perhapps you'll spot my mistakes easily. I'm leaving some commented lines so you can maybe see what I did wrong in a previous attempt. Thanks for your input!

#ifndef DESCRIPTIONFILEITEM_H
#define DESCRIPTIONFILEITEM_H

#include <QStandardItem>
#include <Qt>

class DescriptionFileItem :  public QObject, public QStandardItem
{
    Q_OBJECT

public:
    explicit DescriptionFileItem(const QString & text, bool isFileName=false, QObject* parent = 0);

    void setData ( const QVariant & value, int role = Qt::UserRole + 1 );

    QVariant data( int role = Qt::UserRole + 1 ) const;

    QString text;
    Qt::CheckState checkedState;
    bool isFileName;

signals:
    void CheckStateChanged();

public slots:
    void UpdateCheckedStateOnChildStateChanged();

};

#endif // DESCRIPTIONFILEITEM_H   

Corresponding .cpp :

#include "DescriptionFileItem.h"

DescriptionFileItem::DescriptionFileItem(const QString & text, bool isFileName, QObject* parent):
    QObject(parent),QStandardItem(text)
{
    this->isFileName = isFileName;
    checkedState = Qt::Checked;
}

void DescriptionFileItem::setData ( const QVariant & value, int role){

    if(role == Qt::CheckStateRole){
        Qt::CheckState newCheckState = (Qt::CheckState)value.toInt();
        checkedState = newCheckState;
        if(isFileName){
            if(newCheckState == Qt::Unchecked || newCheckState == Qt::Checked){
                for(int i = 0; i<rowCount(); i++){
                    DescriptionFileItem* child = (DescriptionFileItem*)QStandardItem::child(i);
                    QModelIndex childIndex = child->index();
                    child->model()->setData(childIndex,newCheckState, Qt::CheckStateRole);
                    //child->setCheckState(newCheckState);
                    //child->setData(newCheckState,Qt::CheckStateRole);
                }
                /*if(rowCount()>1){
                    emit this->model()->dataChanged(this->child(0)->index(),this->child(rowCount()-1)->index());
                }else{
                    emit this->model()->dataChanged(this->child(0)->index(),this->child(0)->index());
                }*/
            }
        }else{
            emit CheckStateChanged();
        }
        //emit this->model()->dataChanged(this->index(),this->index());
    }else{
        QStandardItem::setData(value,role);
    }
}

QVariant DescriptionFileItem::data( int role ) const{
     if (role == Qt::CheckStateRole){
            return checkedState;
     }
     return QStandardItem::data(role);
}

void DescriptionFileItem::UpdateCheckedStateOnChildStateChanged()
{
    Qt::CheckState min = Qt::Checked;
    Qt::CheckState max = Qt::Unchecked;
    Qt::CheckState childState;
    for(int i = 0; i<rowCount(); i++){
        DescriptionFileItem* child = (DescriptionFileItem*)QStandardItem::child(i);
        childState = (Qt::CheckState) child->data(Qt::CheckStateRole).toInt();
        min = min>childState ? childState: min;
        max = max<childState ? childState: max;
    }
    if(min >= max)
        setData(min, Qt::CheckStateRole);
    else
        setData(Qt::PartiallyChecked, Qt::CheckStateRole);
}  

And the construction of the connection / tree:

        DescriptionFileItem* descFileStdItem = new DescriptionFileItem(descriptionFileName, true);
        descFileStdItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsTristate);
        descriptionFileSIModel.appendRow(descFileStdItem);
        typedef pair<string,int> indexType;
        foreach(indexType index,dataFile->indexes){
            DescriptionFileItem* key_xItem = new DescriptionFileItem(index.first.c_str());
            descFileStdItem->appendRow(key_xItem);
            key_xItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
            QObject::connect(key_xItem,SIGNAL(CheckStateChanged()),descFileStdItem,SLOT(UpdateCheckedStateOnModelDataChanged()));
        }

EDIT: final answer, thanks to stu (see below)

void DataLoadWidget::ModelItemChanged(QStandardItem *item)
{
    QStandardItem* parent = item->parent();
    if(parent == 0){
        //folder state changed--> update children if not partially selected
        Qt::CheckState newState = item->checkState();
        if(newState != Qt::PartiallyChecked){
            for (int i = 0; i < item->rowCount(); i++)
            {
                item->child(i)->setCheckState(newState);
            }
        }
    }
    else{//child item changed--> count parent's children that are checked
        int checkCount = 0;
        for (int i = 0; i < parent->rowCount(); i++)
        {
            if (parent->child(i)->checkState() == Qt::Checked)
                checkCount++;
        }

        if(checkCount == 0)
            parent->setCheckState(Qt::Unchecked);
        else if (checkCount ==  parent->rowCount())
            parent->setCheckState(Qt::Checked);
        else
            parent->setCheckState(Qt::PartiallyChecked);
    }
}
Sam
  • 117
  • 11

1 Answers1

1

Unless I've misunderstood your question it seems that your solution is massively over-complicated. You should be able to do this trivially with the default QStandardItemModel implementation.

How about something like this (error handling omitted)?

QObject::connect(model, SIGNAL(itemChanged(QStandardItem*)), someObject, SLOT(modelItemChanged(QStandardItem*)));

And then in the signal handler:

void modelItemChanged(QStandardItem* item)
{
    QStandardItem* parent = item->parent();
    int checkCount = 0;
    int rowCount = parent->rowCount();

    for (int i = 0; i < rowCount; i++)
    {
        if (parent->child(i)->checkState() == Qt::Checked)
            checkCount++;
    }

    switch (checkCount)
    {
    case 0:
        parent->setCheckState(Qt::Unchecked);
        break;
    case rowCount:
        parent->setCheckState(Qt::Checked);
        break;
    default:
        parent->setCheckState(Qt::PartiallyChecked);
    }
}

This is by no means optimal but it may be good enough for your purposes.

Stu Mackellar
  • 11,510
  • 1
  • 38
  • 59
  • Thank you very much, this is indeed a much better approach. I'll post the final code below for reference. Some very minor corrections: the `case parent->rowCount():` was problematic (not const in a switch, apprently that's a problem.. `child(i)` returns a pointer.. and the parent -> child propagation. I'm still a bit curious about why my signal / slot connection didn't work before (though I agree the design was inferior anyways); if anyone has any comments to offer, please do not hesitate. – Sam Jul 24 '11 at 17:06
  • I typed this from memory so I'm not surprised there were a couple of semantic errors - I've edited to fix. Not sure why your signal/slot connection isn't working but the debug output at runtime will usually tell you exactly what's wrong. Also, you should always check the return value from QObject::connect. That way you'll know immediately if something's not right with the connection. – Stu Mackellar Jul 24 '11 at 17:16
  • Will do check that next time. Thanks! – Sam Jul 25 '11 at 09:23