Results 1 to 5 of 5

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

  1. #1
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    22
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:isplayRole);
    //model->setData(index,QVariant(Qt::Unchecked),Qt::CheckSt ateRole);
    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(o n_treeView_clicked(QModelIndex)));


    }

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

    void extension:n_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);
    }

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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Performing some action on checking the checkbox and also updating the checkbox st

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

  3. #3
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    22
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Performing some action on checking the checkbox and also updating the checkbox st

    can you help me with a sample code...
    it would be very grateful of you.....

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Performing some action on checking the checkbox and also updating the checkbox st

    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

  5. #5
    Join Date
    May 2011
    Location
    Mumbai, India
    Posts
    22
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Performing some action on checking the checkbox and also updating the checkbox st

    thanx........

Similar Threads

  1. Replies: 5
    Last Post: 27th May 2011, 02:51
  2. checkbox
    By fluefiske in forum Newbie
    Replies: 3
    Last Post: 5th October 2010, 22:41
  3. Checkbox save state.
    By Project25 in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2009, 23:31
  4. Accessing check state of CheckBox in QTableWidget
    By lnxusr in forum Qt Programming
    Replies: 6
    Last Post: 22nd November 2009, 01:13
  5. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.