PDA

View Full Version : Why does the table cell containing checkbox becomes solid when it is clicked?



lornazh
8th April 2010, 05:17
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.

44864487

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;

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

GimpMaster
19th May 2010, 16:10
I'm using 4.6.0 and it worked good for me.