1

I want to use a QTableView. This is a result of some tests.

TableView with 4 cells, every cell contains a misterious checkbox-like box

As you can see, there are some boxes in every cell, before the content "123". What are these boxes and how can I remove these?

I think I need to change some properties of the QTableView, but I did not found a property related to these misterious boxes. Here some code I used:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    //...

    TVLDataModel* model = new TVLDataModel();
    ui->uxTVLView->setModel(model);
}

TVLDataModel (inherits QAbstractTableModel)

int TVLDataModel::rowCount(const QModelIndex &parent) const
{
    return 2;
}

int TVLDataModel::columnCount(const QModelIndex &parent) const
{
    return 2;
}

QVariant TVLDataModel::data(const QModelIndex &index, int role) const
{
    return 123;
}
fedab
  • 978
  • 11
  • 38

2 Answers2

4

you should change your QVariant TVLDataModel::data(const QModelIndex &index, int role) const function to indicate the role you are using. for example Qt::EditRole, Qt::BackgroundRole, etc.

for example :

QVariant TVLDataModel::data(const QModelIndex &index, int role) const
{
    switch(role){
       case Qt::EditRole :
       case Qt::DisplayRole :    
           return 123; 

       default : break;
    }
     return QVariant();
}

Otherwise you would return 123 for every ItemDataRole.

Venom
  • 1,010
  • 10
  • 20
  • 3
    Yes exactly this is the issue. You are essentially returning (Qt::CheckState)123 for the Qt::CheckStateRole. – milianw Apr 12 '17 at 14:45
0

Those "strange" boxes are checkboxes. Your model indicates that each item is checkable.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313