Performing some action on checking the checkbox and also updating the checkbox state.



  • Hii,
    Below is my code..It displays the Dlls with checkboxes in the tree view..
    Now I want to perform some action on it..
    On checking and unchecking the checkbox it should perform an action..and also updated the state of the checkbox each time i start the application, means when i hav checked or unchecked the checkbox, it should show me the same state(i.e. checked or unchecked) when I again start my application the next time..
    Can any one help me plzz....

    /extension.h:->*************************************************/
    @
    #ifndef EXTENSION_H
    #define EXTENSION_H

    #include "standarditemmodel.h"

    #include <QMainWindow>
    #include <QStandardItemModel>

    namespace Ui {
    class extension;
    }

    class extension : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit extension(QWidget *parent = 0);
    ~extension();

    void clicked(QModelIndex index);
    
    
    //Qt::ItemFlags flags() const;
    //virtual QVariant data ( int role) const;
    //virtual void setData ( const QVariant & value, int role);
    

    private:
    Ui::extension *ui;
    //QStandardItemModel *model;
    QStandardItem *item;
    standarditemmodel *model;

    private slots:
    //void on_treeView_clicked(QModelIndex index);
    };

    #endif // EXTENSION_H
    @
    /**************************************************************/

    /extension.cpp->************************************************/
    @
    #include "extension.h"
    #include "ui_extension.h"
    #include "standarditemmodel.h"
    #include <QDir>
    #include <Qt>
    #include <qdebug.h>
    #include <QModelIndex>

    extension::extension(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::extension)
    {

    //model = new QStandardItemModel(0,1,this);
    model = new standarditemmodel(this);
    QString path = "E:/qtpro/codecs";
    QDir dir;
    dir.setPath(path);
    QStringList files;
    files << "*.dll";
    dir.setNameFilters(files);
    QFileInfoList list = dir.entryInfoList();
    for (int i = 0; i < list.size(); ++i)
    {
    QFileInfo fileInfo = list.at(i);

            QModelIndex index = model->index(i,0,QModelIndex());
            item = new QStandardItem();
            item->setText(QString("%0").arg(fileInfo.fileName()));
            item->setCheckable(true);
            //item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
            model->setData(index,QVariant(QString("%0").arg(fileInfo.fileName())),Qt::CheckStateRole && Qt::DisplayRole);
            //model->setData(index,QVariant(Qt::Unchecked),Qt::CheckStateRole);
            model->setItem(i,item);
    }
    
     ui->treeView->setModel(model);
    
    //connect(model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(clicked(//QModelIndex)));
    

    connect(ui->treeView,SIGNAL(clicked(QModelIndex)),this,SLOT(on_treeView_clicked(QModelIndex)));

    }

    extension::~extension()
    {
    delete ui;
    }

    void extension::on_treeView_clicked(QModelIndex index)
    {
    QStandardItem item1 = model->itemFromIndex(index);
    qDebug()<< item1->text();
    }
    @
    /
    *************************************************************/

    /standarditemmodel.h:->******************************************/
    @
    #ifndef STANDARDITEMMODEL_H
    #define STANDARDITEMMODEL_H

    #include <QStandardItemModel>
    #include <QSet>
    #include <QPersistentModelIndex>

    class standarditemmodel : public QStandardItemModel
    {
    Q_OBJECT
    public:
    explicit standarditemmodel(QObject *parent = 0);
    QSet<QPersistentModelIndex> set;

    signals:
    void dataChanged(QModelIndex, QModelIndex);

    public slots:

    virtual QVariant data ( const QModelIndex & index, int role) const;
    virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
    virtual bool setData ( const QModelIndex & index, const QVariant & value, int role);
    

    };

    #endif // STANDARDITEMMODEL_H
    @
    /***************************************************************/

    /standarditemmodel.cpp********************************************/
    @
    #include "standarditemmodel.h"
    #include <QSet>
    #include <QPersistentModelIndex>

    standarditemmodel::standarditemmodel(QObject *parent) :
    QStandardItemModel(parent)
    {

    }

    QVariant standarditemmodel::data(const QModelIndex& index, int role) const
    {

    if (index.isValid() && index.column()==0 && role == Qt::CheckStateRole)
    

    return set.contains(index) ? Qt::Checked : Qt::Unchecked;
    return QStandardItemModel::data(index, role);
    }

    Qt::ItemFlags standarditemmodel::flags(const QModelIndex& index) const
    {
    return QStandardItemModel::flags(index) | Qt::ItemIsUserCheckable;
    }

    bool standarditemmodel::setData(const QModelIndex& index, const QVariant& value, int role)
    {

    if (index.isValid() && index.column()==0 && role == Qt::CheckStateRole)
    {
    if (value == Qt::Checked) set.insert(index);
    else set.remove(index);
    emit dataChanged(index, index);
    return true;
    }
    return QStandardItemModel::setData(index, value, role);
    }
    @

    [edit : code highlighted / Denis Kormalev]




Log in to reply
 

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