PDA

View Full Version : Unsolicited checkboxes in QTableView



MattPhillips
19th December 2009, 20:37
Hi,

I'm using QAbstractTableModel/QTableView to display a table of numeric data stored as strings that have been cast to QVariant--that is, in a vector<QVariant> in my QAbstractTableModel subclass. When I show the data, in every cell there is a checkbox in tri-state mode--checked when the value is 2, darkened when it is 1, and empty otherwise. :confused::confused:
In no place did I (explicitly) 'ask' for this checkbox, I just want to display the data in the cell all by itself. Here is the code I use to set the QTableView properties:

(DataTable is a QWidget, saTableModel is my QAbstractTableModel subclass)


DataTable::DataTable(QWidget* parent, saTableModel* _satm) : QWidget(parent), satm(_satm)
{
QVBoxLayout* layMain = new QVBoxLayout;
tblv = new QTableView(this);
layMain->addWidget(tblv);
setLayout(layMain);

SetUpTableView();

hdrv = new QHeaderView(Qt::Horizontal,tblv);
tblv->setHorizontalHeader(hdrv);
hdrv->resizeSections(QHeaderView::ResizeToContents);

tblv->show();
}
void DataTable::SetUpTableView()
{
tblv->setModel(satm);
tblv->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
tblv->setAlternatingRowColors(true);
QItemSelectionModel* select = new QItemSelectionModel(satm);
tblv->setSelectionModel(select);
tblv->setSelectionBehavior(QAbstractItemView::SelectRows );
}

Anybody know why I'm getting unwanted checkboxes?

Best,
Matt

wysota
19th December 2009, 21:04
Your model is incorrect. You are not returning empty QVariants for Qt::CheckStateRole in your implementation of data(). You probably don't differentiate roles at all.

MattPhillips
19th December 2009, 21:44
wysota,

Right you were! Adding 'if (role==QtDisplayRole) return ...; return QVariant();' solved the problem. And your answer came in time for my deadline too, thanks!

Matt