PDA

View Full Version : Problems with custom model and rowsInserted() signal



frank100
31st March 2011, 10:47
Hi everybody,

I am having a problem with a custom model. I started with a code where a QTreeView was connected to a customized QStandardItemModel. Everything worked great until an idiot (me) wanted to implement a custom proxy from QAbstractProxyModel and messed things up.

In order to get the new insertions from the model, I connected the rowsInserted() signal:



TreeProxy::TreeProxy ( QStandardItemModel * parent ) :
QAbstractProxyModel(parent)
{
setSourceModel( parent );

bool ok;
ok = connect( sourceModel(),
SIGNAL (rowsInserted ( const QModelIndex & , int , int ) ),
this,
SLOT (slot_inserted_rows_for_my_proxy ( const QModelIndex & , int , int ) ));
Q_ASSERT(ok)


The first thing I noticed was that the model was not sending the rowsInserted() although the view receives new insertions with no problem at all. The model inherits from QStandardItemModel not from QAbstractItemModel and it does not reimplement appendRow()/insertRow() so I would expect the signal to be OK

I noticed that the proxy receives the signal doing this:



beginInsertRows(
indexFromItem(parent),
indexFromItem(child_node).row(),
indexFromItem(child_node).row()
);
parent->appendRow(child_node);
endInsertRows();


But I would expect the begin/end stuff to be done inside the inherited implementation of appendRow. With that the proxy works and the view fails (blank)

My only solution so far was redeclaring rowsInserted() as a public signal and emit it manually. According the documentation and other questions in this forum, I must not do this. So I wonder what I am doing wrong? What else do I have to do to synchronize a custom proxy and a custom model that inherits a full model implementation?

thanks!

wysota
31st March 2011, 11:35
Is the problem about insertions to the proxy not propagating to the source or the other way round?

frank100
31st March 2011, 13:48
it is from the model insertions not propagating to the proxy

My expectation is that calling insertRow() or appendRow() from a derived model from QStandardItemModel that does not re-implement those functions, should raise the rowsInserted() signal

should it be like that? For me it seems strange to wrap around appendRow() the begin/end insert rows.

wysota
31st March 2011, 13:53
The signal is emitted. At least according to the source code :)


if (model)
model->d_func()->rowsInserted(q, row, count);
rowsInserted() calls endInsertRows() which emits the signal.