Maschina
Maschina

Reputation: 926

Qt: Implementing span of QAbstractItemModel

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.

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).

Can assist me on how to implement that function, so that I do not have to use setSpan from within view controller?

Upvotes: 3

Views: 2210

Answers (1)

Maschina
Maschina

Reputation: 926

Thanks to Daniel Castro, I solved this as the following:

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());
        }
    }
}

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

Upvotes: 3

Related Questions