Elmi
Elmi

Reputation: 6203

Disable (grey-out) some rows in a QTreeView

I have a (very simple yet) QTreeView showing some rows:

m_cameraModel = new QStandardItemModel(this);
QSortFilterProxyModel* cameraProxyModel = new QSortFilterProxyModel(this);
cameraProxyModel->setSourceModel(m_cameraModel);
ui.CameraTreeView->setModel(cameraProxyModel);

m_cameraModel->appendRow(new QStandardItem("Panavision"));
m_cameraModel->appendRow(new QStandardItem("Panaflex"));

Here I want to disable the first row "Panavision" so that it is still visible but can't be selected any more and is somehow greyed-out so that the user can see this entry is not active.

May be this is some kind of beginner-question, but how can this be done?

Thanks!

Upvotes: 0

Views: 2395

Answers (2)

vahancho
vahancho

Reputation: 21220

I would try to do that in the following way:

// Get item that corresponds to the first row
QStandardItem *item = m_cameraModel->item(0, 0);
// Disable the item.
item->setFlags(Qt::NoItemFlags);

Upvotes: 4

Nicholas Smith
Nicholas Smith

Reputation: 11754

You'd want to use the QItemDelegate class, which allows you to disable the row you want to amongst other things. There's a good question here on StackOverflow that shows how to do a very basic example: How to set a delegate for a single cell in Qt item view?

Upvotes: 0

Related Questions