29

I'm just getting started with Qt programming, and I'm trying to make a simple tabular data layout using a QTableView control with a model class of my own creation inheriting from QAbstractTableModel. For some reason, my table view ends up looking like this:

alt text
(source: nerdland.net)

What in the heck are those things that look like checkboxes (but don't do anything when I click them) in every cell, and how do I make them go away? I haven't changed any of the QTableView properties except for the object's name.

If it matters, my model code is dead simple:

MyTableModel::MyTableModel(QObject* parent)
  : QAbstractTableModel(parent)
{
}

MyTableModel::~MyTableModel()
{
}

int MyTableModel::rowCount(const QModelIndex& parent) const
{
  return 1000; 
}

int MyTableModel::columnCount(const QModelIndex& parent) const
{
  return 5;
}

QVariant MyTableModel::data(const QModelIndex& index, int role) const
{
  return "Foo";
}

The dialog UI is built in Qt Designer, and inside the class for the dialog I attach the model to the view like this:

MyTableModel testModel = new MyTableModel(this);
ui.testTable->setModel(testModel);

Other than that I perform no operations on ui.testTable.

Using Qt 4.6.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166

2 Answers2

57

Try changing MyTableModel::data() to the following:

QVariant MyTableModel::data(const QModelIndex& index, int role) const
{
    if (role == Qt::DisplayRole)
        return "foo";
    else
        return QVariant();
}

Probably the returned QVariant for role Qt::CheckStateRole was misunderstood by the QTableView.

Elrohir
  • 1,456
  • 14
  • 12
  • 1
    Perfect! Thank you. I suppose I shouldn't just be ignoring parameters that I haven't learned about yet... – Tyler McHenry Mar 07 '10 at 16:33
  • 3
    Glad to help you :) Qt simply rocks! – Elrohir Mar 07 '10 at 16:36
  • 2
    Thanks for your last sentence "Probably the returned QVariant for role Qt::CheckStateRole was misunderstood by the QTableView." :) As I wanted to enable editing for my QTableView, I disabled my role == Qt::DisplayRole check and got those silly checkboxes. Thanks to your comment, I'm now explicitly checking for Qt::DisplayRole AND Qt::EditRole and returning QVariant() in other cases. – Charl Botha Feb 08 '13 at 15:32
  • 1
    In case someone out there is using pyqt with QVariant version 2 where there is no class QVariant at all, returning None will do the trick – architectonic Dec 15 '15 at 12:07
2

Do you by any chance happen to set the Qt::ItemIsUserCheckable flag in flags()?

Macke
  • 24,812
  • 7
  • 82
  • 118
  • 1
    No, and even if I override `flags` to return ` QAbstractTableModel::flags(index) & ~Qt::ItemIsUserCheckable;`, the checkboxes remain. – Tyler McHenry Mar 07 '10 at 16:16