I have a table view which uses a customized model. In the table view, there are many check boxes. Clicking any of the check boxes will make the parent cell becomes solid. The check state is saved though. Clicking some other cell will make it back to normal.
solid cell..PNGback to normal..PNG
It seems the issue is new in Qt 4.5 and 4.6, it was good in version 4.3.
The source code of the example is attached. (Why cannot I attach a zip file?) The application is built using Qt 4.6.2, on Windows platform.
Cannot upload tablemodel.h either. So I copy code directly.
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QtCore/QAbstractTableModel>
static const int ROWS = 2;
static const int COLS = 2;
{
Q_OBJECT
public:
{
}
{
switch(role)
{
case Qt::CheckStateRole:
return enabled_[index.row()][index.column()] ? Qt::Checked : Qt::Unchecked;
default:
break;
}
}
{
switch(role)
{
case Qt::CheckStateRole:
if (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked)
enabled_[index.row()][index.column()] = true;
else
enabled_[index.row()][index.column()] = false;
return true;
default:
break;
}
}
virtual Qt
::ItemFlags flags
(const QModelIndex &index
) const {
}
private:
bool enabled_[ROWS][COLS];
};
#endif // TABLEMODEL_H
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QtCore/QAbstractTableModel>
static const int ROWS = 2;
static const int COLS = 2;
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
TableModel(QObject *parent = 0)
: QAbstractTableModel(parent)
{
}
int rowCount(const QModelIndex &parent = QModelIndex()) const { return ROWS; }
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const { return COLS; }
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
switch(role)
{
case Qt::CheckStateRole:
return enabled_[index.row()][index.column()] ? Qt::Checked : Qt::Unchecked;
default:
break;
}
return QVariant();
}
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
{
switch(role)
{
case Qt::CheckStateRole:
if (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked)
enabled_[index.row()][index.column()] = true;
else
enabled_[index.row()][index.column()] = false;
return true;
default:
break;
}
return QAbstractTableModel::setData(index, value, role);
}
virtual Qt::ItemFlags flags(const QModelIndex &index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
private:
bool enabled_[ROWS][COLS];
};
#endif // TABLEMODEL_H
To copy to clipboard, switch view to plain text mode
Bookmarks