【问题标题】:Qt: Implementing span of QAbstractItemModelQt:QAbstractItemModel 的实现跨度
【发布时间】:2013-09-17 03:49:53
【问题描述】:

我想知道如何在 Qt 框架中实现自己的 QAbstractItemModel::span 函数?我知道Qt5还没有实现那个功能。

我尝试为我自己编写的模型重新实现该函数,并通过使用 span() 来利用 span 功能。 第一次尝试根本没有成功。因此,我在该重新实现的函数中设置了一个断点。我意识到 Qt 永远不会触发该函数(没有命中断点)。

可以帮助我如何实现该功能,这样我就不必在视图控制器中使用 setSpan 了吗?

【问题讨论】:

  • 文档说 QAbstractItemModel::span() 当前未使用,所以我认为您必须修改 QTableView (或您正在使用的任何视图),以便它使用模型中的 span() 跨度>

标签: c++ qt


【解决方案1】:

感谢 Daniel Castro,我通过以下方式解决了这个问题:

重新实现 QAbstractItemView 的 setModel:

void View_DndLinBatch::setModel(QAbstractItemModel *model)
{
    QTableView::setModel(model);

    for (int row = 0; row < this->model()->rowCount(); row++)
    {
        for (int col = 0; col < this->model()->columnCount(); col++)
        {
            QSize span = this->model()->span(this->model()->index(row, col));
            this->setSpan(row, col, span.height(), span.width());
        }
    }
}

并重新实现 QAbstractItemModel 的 span 函数:

QSize model_DndLinBatch::span(const QModelIndex &index) const
{
    if (index.column() == 0)
    {
        return QSize(2,1);
    }
    else
    {
        return QAbstractItemModel::span(index);
    }
}

【讨论】:

  • 请记住,此解决方案仅在模型保持不变时才有效。模型可能会发生变化,从而导致跨度也发生变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
相关资源
最近更新 更多