Qt:实现QAbstractItemModel的跨度 [英] Qt: Implementing span of QAbstractItemModel
问题描述
我想知道如何在Qt框架中实现自己的QAbstractItemModel :: span函数?我知道Qt5还没有实现该功能。
I was wondering how to implement a own QAbstractItemModel::span function in Qt framework? I know that Qt5 did not yet implemented that function yet.
我试图重新实现该函数为我的自写模型和利用span功能通过使用span()。
第一次尝试没有工作。因此,我在重新实现的函数中设置了一个断点。我意识到,Qt从来没有触发该函数(断点没有命中)。
I tried to reimplement that function for my self-written model and utilize span capabilities by using span(). First try didn't work out at all. Therefore, I set a breakpoint inside that reimplemented function. I realized that Qt never triggers that function (breakpoint was not hit).
可以帮助我如何实现这个函数,所以我不必使用setSpan在视图控制器中?
Can assist me on how to implement that function, so that I do not have to use setSpan from within view controller?
推荐答案
感谢丹尼尔·卡斯特罗,我解决了以下问题:
Thanks to Daniel Castro, I solved this as the following:
重新实现QAbstractItemView的setModel:
Reimplementing setModel of QAbstractItemView:
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函数:
and reimplementing span function of QAbstractItemModel:
QSize model_DndLinBatch::span(const QModelIndex &index) const
{
if (index.column() == 0)
{
return QSize(2,1);
}
else
{
return QAbstractItemModel::span(index);
}
}
这篇关于Qt:实现QAbstractItemModel的跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!