Ste*_*mer 4 c++ qt qtreeview qabstractitemmodel
我正在寻找一种更好的方法来在树中显示多级分层数据,其中每列的含义根据树中的级别而变化。
我正在使用QTreeView和QAbstractItemModel显示我的模型数据。
每个模型行具有不同数量的列和不同的列名称,具体取决于其在层次结构中的级别。
为了给树中显示的数据提供上下文,我需要为层次结构中的每个级别都有列标题。
问题是QTreeView只有 1 组列标题。
当前方法
目前,每次选定的行更改时,我都会更改标题。
我通过连接到树视图的selectionModel,并在QModelIndex每次选择更改时发出带有新的信号
void Window::slotOnSelectionChanged(const QItemSelection& new_selection, const QItemSelection& old_selection)
{
QItemSelectionModel* selection_model = _view->selectionModel();
QModelIndex index = selection_model->currentIndex();
if (index.isValid())
emit selectedIndexChanged(index);
}
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我连接到这个信号,当它触发时,存储选定的行,并强制更新列标题
void Model::slotOnSelectedIndexChanged(QModelIndex index)
{
assert(index.isValid());
_selected_row = modelRow(index);
emit headerDataChanged(Qt::Horizontal, 0, _root->numColumns());
}
Run Code Online (Sandbox Code Playgroud)
在QAbstrateItemModel::headerData回调中,我然后使用selected_row获取当前选定行的标题
QVariant Model::headerData(int i, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
switch (orientation)
{
case Qt::Horizontal:
return QVariant(_selected_row->header(i));
...
Run Code Online (Sandbox Code Playgroud)
结果
结果如下所示 - 请注意列标题如何随着所选行的变化而变化。
问题
仅通过查看视图每个数据是什么并不是立即显而易见的,因此用户需要更改行以查看每列的实际含义。
我想要的是有某种嵌入的列标题行,层次结构中的每级 1 个。
像这样的东西:
问题
在@Kuba Ober的建议下,我在树的每个层次结构中的位置 0 处添加了一个额外的行。它没有孩子。
然后将模型配置为 的特殊情况index.row() == 0,知道该行是标题行而不是数据行。
例如:在Model::flags标题行中是不可编辑的
Qt::ItemFlags Model::flags(const QModelIndex& index) const
{
Qt::ItemFlags item_flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
// header row is not editable
if (index.row() != 0)
item_flags |= Qt::ItemIsEditable;
return item_flags;
}
Run Code Online (Sandbox Code Playgroud)
我现在返回空字符串,headerData因为标题在树本身中
QVariant Model::headerData(int i, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
switch (orientation)
{
case Qt::Horizontal:
return QVariant(); // no column header, it's already in the tree
...
Run Code Online (Sandbox Code Playgroud)
我还更改了标题的背景颜色,使其脱颖而出
QVariant Model::data(const QModelIndex& index, int role) const
{
switch (role)
{
case Qt::BackgroundColorRole:
if (index.row() == 0) // header row
return QColor(Qt::darkGray);
break;
...
Run Code Online (Sandbox Code Playgroud)
结果几乎正是我想要的