views:

255

answers:

2

Hey,

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?

A: 

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.

Kaleb Pederson
this did not work, the single child returned from parent.child(start,0) is invalid, and the model returned is NULL.
David Menard
The above will work with a well-behaving model. Check out ModelTest on the Qt Labs site. In addition, take a look at the model's implementation of createIndex() and/or use a debugger to figure out what's going on.
Kaleb Pederson
A: 

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);
   }
}
Rhett D
Yes, that should work if you're only looking at the top level. Don't forget to include parent on the index() calls if you have a tree structure more than one level deep.
Kaleb Pederson
thats the workaround I started implemented, altough it would have been nice to not have to do it, thanks
David Menard