PDA

View Full Version : Performing some action on checking the checkbox and also updating the checkbox state.



Ketan Shah
29th May 2011, 21:43
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::CheckSta teRole);
model->setItem(i,item);
}

ui->treeView->setModel(model);

//connect(model,SIGNAL(dataChanged(QModelIndex,QMode lIndex)),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);
}

/************************************************** ************/

Santosh Reddy
29th May 2011, 22:00
You need to save the checked/unchecked status of each item in your tree. You can use QSettings to save configuration data, and retrieve them when you restart the application.

You can save the state into QSettings in standarditemmodel::setData(), and get them back in standarditemmodel::data()

Ketan Shah
30th May 2011, 07:55
can you help me with a sample code...
it would be very grateful of you.....

stampede
30th May 2011, 09:10
If you've written this code yourself, then you are able to accomplish this task without problems.
Ask precise questions if you want help. And don't expect someone will do the work for you, it's your homework.

can you help me with a sample code
Sure, even Qt docs can help you: QSettings basic usage (http://doc.qt.nokia.com/stable/qsettings.html#details)

Ketan Shah
1st June 2011, 15:54
thanx........