PDA

View Full Version : Delete if row checked



wirasto
10th July 2009, 21:06
I try created my own model for add checkbox in first column tableview.



#ifndef MODELANGGOTA_H
#define MODELANGGOTA_H

#include <QSqlQueryModel>

class modelAnggota : public QSqlQueryModel
{
Q_OBJECT

public:
modelAnggota(QObject *parent = 0);
QVariant data(const QModelIndex &item, int role) const;

};

#endif // MODELANGGOTA_H






#include "modelanggota.h"

modelAnggota::modelAnggota(QObject *parent)
: QSqlQueryModel(parent)
{

}

QVariant modelAnggota::data(const QModelIndex &index, int role) const
{
if (role == Qt::CheckStateRole && index.column() == 0)
return Qt::Unchecked;

return QSqlQueryModel::data(index, role);
}





QSqlQueryModel *model = new modelAnggota();
model->setQuery("select * from mailbox");

ui->tableView->setModel( model );
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->tableView->setAlternatingRowColors(TRUE);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;



But checkbox can't checked or unchecked. I need the checkbox not for edited data on table (database), but just for checked. Then, if I Click a pushButton (delete button), row with checked will remove. And yes, row in database table will removed too.


Have any suggestion ?

Sorry. my english is bad :)

ChrisW67
11th July 2009, 04:27
You should look at the QAbstractItemModel::flags method and return Qt::ItemIsUserCheckable and possibly some other flags for the check box column.

wirasto
11th July 2009, 15:16
You mean like this ? Ah, not work.



Qt::ItemFlags modelAnggota::flags(const QModelIndex &index) const
{
if (index.column()==0)
return Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsSel ectable|Qt::ItemIsUserCheckable;


return QAbstractItemModel::flags(index);

}