0

I'm trying to add always-empty last row. Now I have proxy model, on which I'm calling insertRow() - the row is inserted. But I need to add another row, when last row is updated (data is inserted). To which signal should I connect?

Weird error occurs sometimes - when clicking on existing row, data are copied to last row. Do you have idea what can be wrong?

Thanks for help

myky
  • 55
  • 1
  • 5

2 Answers2

1

You might want to connect a slot to the QSqlTableModel::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ) signal.

Chris Browet
  • 4,156
  • 20
  • 17
  • I was using that signal to evaluate code like this: `if(end.row() == this->rowCount() - 1) { qDebug() << "New record added"; this->insertRow(end.row()); }` but then, when I am cliking on table, data are copied. – myky Mar 06 '12 at 19:27
0

I've tried that.

There are a couple problems that made me rethink how to implement this:

  1. How QSqlTableModel handles internally the buffer (d pointer you cant access even when subclassing) is the reason you're seeing weird things that copy data to last row.

  2. You cannot distinguish table row count and underlying model rowcount. They're always the same, you would have to use some flags to know when you already have a new row and dont want to add another (dataChanged is emitted when changing any row) ). There are, at least, 4 signals you would have to connect to keep these flags updated:

     void   beforeDelete ( int row )
     void   beforeInsert ( QSqlRecord & record )
     void   beforeUpdate ( int row, QSqlRecord & record )
     void   primeInsert ( int row, QSqlRecord & record )
    
  3. If you have more than one columns. Which submit method have you chosen? (manual, onrowchange... etc) Depending on this you would have to decide when adding a new blank row. (any error on last changes?)

Not imposible, but ugly.

Model and Table are too coupled to find an easy solution for this. I ended up using a form under the table to add new data.

I would be better to reimplement you own model and/or table than using the ones Qt provide to do this.

trompa
  • 1,967
  • 1
  • 18
  • 26