Search code examples
qtsignals-slotsqabstractitemmodel

Help getting inserted data after Qt's rowInserted signal


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?


Solution

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