Mikhail
Mikhail

Reputation: 8028

How to get insertRows source?

I am instrumenting a model/view for item rearrangement and I am failing to understand how to override the insertRows method. Just for practice I am trying to wrap a std::vector with a custom structure.

std::vector<aStruct> mD;//my data
bool insertRows(int start, int rows, const QModelIndex & parent)
{
    auto i = parent.row();
    cout <<"I going to " << start << ":" << rows << " choosing "<< i<< endl;
    beginInsertRows(parent, start, start + rows - 1);
    aStruct blank(0);// Should be the value of the item being moved?
    mD.insert(mD.begin()+start,blank);
    endInsertRows();
    return true;
}

Unfortunately, I can't seem to find a place to get at element that gives me a hold of the item being moved. How do I do this?

Upvotes: 0

Views: 100

Answers (2)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

You should use next steps to insert rows:

1. Call beginInsertRows
2. Modify your internal data structure
3. Call endInsertRows

There is all Ok in your sample.

View will insert empty rows (as @Riateche told) and will fill it automatically, after call of endInsertRows. All that you need is to override YourModel::data method to return correct data from you mD structure.

View will call YourModel::data method immediately after inserting empty rows. You don't need to do any extra operations. View will care about "filling" it.

Overriding of YourModel::setData method is mostly used for interaction between view and model, when user want to change data throught view widget.

Upvotes: 0

Pavel Strakhov
Pavel Strakhov

Reputation: 40502

I assume the mD and insertRows are members of a custom model class.

insertRows doesn't receive any information about contents of inserted rows. Empty values should be inserted. Rows should be filled with data in the setData virtual method implementation.

Upvotes: 1

Related Questions