QModelIndexList indexes = treeView->selectedIndexes(); if (!indexes.isEmpty()) { QModelIndex index = indexes.first(); QString path = fileSystemModel->filePath(index); qDebug() << "Selected file path: " << path; }
QVariant UserModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (index.row() >= m_users.size() || index.row() < 0) return QVariant(); if (role == Qt::DisplayRole) { switch (index.column()) { case 0: return m_users.at(index.row()).name; case 1: return m_users.at(index.row()).age; default: return QVariant(); } } return QVariant(); }In this example, we have a custom model called UserModel that represents a list of users. The data() method of the model is responsible for returning the data that should be displayed in the QTableView widget. We check if the index is valid, and if it is within the bounds of the list of users. If the role of the data is Qt::DisplayRole, we return the appropriate data for the specified column. Otherwise, we return an empty QVariant. Package library: Qt Core module of the Qt library.