在QTreeView中维护checkedstatus继承

时间:2011-07-24 14:09:09

标签: c++ model-view-controller qt qtreeview qstandarditemmodel

我正在尝试做一些基本的事情:你有一个QTreeView。第一深度仅为文件夹,第二深度仅为文件。我希望在每个项目旁边都有一个选中状态的复选框。文件可以选中也可以不选中,文件夹也可以根据文件进行部分检查;总而言之,我相信自然。

我应该去的方式是使用QStandardItemModel并使用自定义子类QStandardItem填充它:DescriptionFileItem。也许这是一个坏主意,如果有一个更简单的方法请惹恼我。

我尝试使用信号和插槽,以便文件上的信号CheckStateChanged连接到其包含文件夹上的插槽UpdateCheckedStateOnChildStateChanged。这需要DescriptionFileItem继承QObject(BTW,我很惊讶QStandardItem没有从QObject继承。我最初希望这可以与提供的基类无缝地工作,但它没有:emitDataChanged()似乎没有触发我的模型的dataChanged()信号......

直接使用模型的dataChanged信号也不起作用:它的调用受到保护,所以你不能在没有子类化的情况下使用它(我认为这是我的下一步行动,除非有人可以帮助我做到正确)。

此刻我有一个信号 - >插槽连接不起作用,我不明白为什么;编译和链接工作正常。这是代码; perhapps你会很容易发现我的错误。我要留下一些注释行,这样你就可以看到我之前尝试过的错误。感谢您的输入!

#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   

对应的.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);
}  

连接/树的构造:

        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()));
        }

编辑:最终答案,感谢stu(见下文)

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);
    }
}

1 个答案:

答案 0 :(得分:1)

除非我误解了你的问题,否则你的解决方案似乎过于复杂。您应该能够使用默认的QStandardItemModel实现轻松完成此任务。

这样的事情怎么样(省略错误处理)?

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

然后在信号处理程序中:

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);
    }
}

这绝不是最佳选择,但它可能足以满足您的目的。