Łukasz Przeniosło Łukasz Przeniosło - 4 years ago 290
C++ Question

QtableView set whole row as readonly

I am having problems setting the whole row of a

to read only. When I use
method it only makes the first column readonly. Here is an example, I am adding a new row to the table and trying to make the whole row readonly:

void CItemsMenu::addSlave(const quint8 addr, const QString& uniqId, const QString& userString)
// create new item
QStandardItem* item = new QStandardItem(m_columnLabels.size()); // size is 3

// make item readonly
item->setEditable(false); // this makes only the 1st column readonly

// append new item
int row = mp_itemsModel->rowCount() - 1;

// slave address
row, (int)itemsTableCol::slaveAddr, QModelIndex()), addr, Qt::EditRole);

// unique ID
row, (int)itemsTableCol::uniqId, QModelIndex()), uniqId, Qt::EditRole);

// user string
row, (int)itemsTableCol::userStr, QModelIndex()), userString, Qt::EditRole);

I would appreciate all help.

Edit: solution:

QList<QStandardItem*> itemsList;
for (int i = 0; i < TML_CAN_SEND_REG_COL; i++)
itemsList.append(new QStandardItem(1));

Answer Source

You need one item per cell.

When you call:


you only set the item for the first column. Quoting Qt documentation:

When building a list or a tree that has only one column, this function provides a convenient way to append a single new item.

When dealing with a QTableView you should be calling the overload of appendRow() that takes a QList<QStandardItem *>. Like this:

QList<QStandardItem *> list;
... // Fill list and set all items in the list to be read-only.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download