PDA

View Full Version : how to get notified when a new row is added into a QTableView ?



aresa
21st October 2013, 08:39
hi,
i need to do some operation to every row of a QTableView, but the number of rows is varying from time to time, thus i need to get notified when row count changes in a QTableView?
how can i do that ?

i tried the following, but 'rowCountChanged' is never called.

class TableView : public QTableView
{
Q_OBJECT

public:

TableView(QWidget *parent = 0) : QTableView(parent)
{

}

protected Q_SLOTS:

void rowCountChanged(int oldCount, int newCount)
{
/// CP1
}
};

pkj
21st October 2013, 11:01
Are you trying some excel like functionality, which has a star as last element, which you can click to add another row?
Or you just need a button, clicking on which adds a row to the table?
In case 1, you need to derive class from QHeaderView, and set the new Qheaderview in the tableview.
Case2 is simpler. route the signal from button add to the model, and follow the procedure to add row at a position.

MarekR22
21st October 2013, 14:19
You doing that wrong. Read about signal slot mechanism since apparently you do not understand that.
I'm suspecting something like:

class MyMainWindow : public QMainWindow {
Q_OBJECT

public slots:
void onRowsNuberChanged();
};

MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent) {
...
connect(ui->tableView->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(onRowsNuberChanged()));
connect(ui->tableView->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(onRowsNuberChanged()));
}

void MyMainWindow::onRowsNuberChanged() {
int rowsCount = ui->tableView->model()->rowCount();
// do whatever you need
...
}

aresa
21st October 2013, 14:32
Ah, i just found QAbstractItemModel provides such a signal as rowadded. that's almost what i want.
it is the model not the view that emits such a signal. thanks.