Reputation: 2321
I have a onText method that connects to a QAbstractItemModel's rowsInserted SIGNAL so I can be notified when new rows have been inserted:
QObject::connect(model, SIGNAL(rowsInserted ( const QModelIndex & , int , int ) ),
client_,SLOT(onText( const QModelIndex & , int , int )) )
The signal works fine, since I am notified when rows are inserted. Here is the onText method:
void FTClientWidget::onText( const QModelIndex & parent, int start, int end )
{
Proxy::write("notified!");
if(!parent.isValid())
Proxy::write("NOT VALID!");
else
Proxy::write("VALID");
QAbstractItemModel* m = parent.model();
}
But I can't seem to be able to get the string from the inserted items. The QModelIndex "parent" passed is NOT VALID, and "m" QAbstractItemModel is NULL. I think its because it's not a actual item, but just a pointer to one? How do I get a hold of the inserted text/elements?
Upvotes: 2
Views: 3042
Reputation: 191
As the parent will be invalid for top level items, another option would be to give FTClientWidget access to the model (if it doesn't violate your intended design), and then FTClientWidget can use the start and end arguments directly on the model itself:
void FTClientWidget::onText( const QModelIndex & parent, int start, int end )
{
//Set our intended row/column indexes
int row = start;
int column = 0;
//Ensure the row/column indexes are valid for a top-level item
if (model_->hasIndex(row,column))
{
//Create an index to the top-level item using our
//previously set model_ pointer
QModelIndex index = model_->index(row,column);
//Retrieve the data for the top-level item
QVariant data = model_->data(index);
}
}
Upvotes: 2
Reputation: 46479
The parent will always be invalid for the top level items, so you can expect it to be invalid. The Qt documentation has a good explanation of exactly how the parent works. start
is the first row at which a child has been inserted, and end
is the last row at which a child was inserted.
Thus, you can access it with something like the following:
int column = 0;
// access the first child
QModelIndex firstChild = parent.child(first, column);
QModelIndex lastChild = parent.child(end, column);
// get the data out of the first child
QVariant data = firstChild.data(Qt::DisplayRole);
Or, if you want, you can use the index to retrieve the model from which you can access it.
Upvotes: 1