Search code examples
qtqt5qt5.5

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!


Solution

  • 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);