PDA

View Full Version : Detect when the content of a cell of QTableView is changed



qt_developer
18th May 2012, 18:48
Hi all,

I need monitoring the changes in the data content of a cell of QTableView (http://qt-project.org/doc/qt-4.8/qtableview.html).

I have a QTableView widget into QMainWindow (http://qt-project.org/doc/qt-4.8/qmainwindow.html).

I have tried with the QAbstractItemView signals (http://qt-project.org/doc/qt-4.8/qabstractitemview.html#signals), the slots are called in my QMainWindow class, but neither of them accomplish the target that I need.

I have tried with the currentChanged( const QModelIndex & current, const QModelIndex & previous ) signal of QItemSelectionModel (http://qt-project.org/doc/qt-4.8/qitemselectionmodel.html#signals), as above the slot is called, but itsn't accomplish the target that I need too.

I make the connects like this:


connect(ui->myTableView->selectionModel(), SIGNAL(currentChanged ( QModelIndex const&, QModelIndex const& )), this, SLOT(onCurrentChanged(QModelIndex con7st&, QModelIndex const&)));
connect(ui->myTableView, SIGNAL(pressed(const QModelIndex &)), this, SLOT(onPressed(const QModelIndex &)));

There are any signal / slot / method or similar that I could use to accomplish my target?

Best regards.

ChrisW67
19th May 2012, 03:45
You are looking in the wrong direction. To get a signal when the data changes you need to be looking at the data model not the view: QAbstractItemModel::dataChanged() for example.

ShapeShiftme
19th May 2012, 07:14
When i first started i tried QTableView and had endless problems myself

I then switched to Qtable Widget. And cell changed events and so forth are much easier to use.

Hope this helps

qt_developer
21st May 2012, 09:59
You are looking in the wrong direction. To get a signal when the data changes you need to be looking at the data model not the view: QAbstractItemModel::dataChanged() for example.

Hi ChrisW67,

I have used this new code:



connect(ui->myTableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));

...

void myClass::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
qDebug() << "the cell has changed";
}


And works fine!

Thank you.

sbabadag
21st August 2021, 11:13
Hi ChrisW67,

I have used this new code:



connect(ui->myTableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));

...

void myClass::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
qDebug() << "the cell has changed";
}


And works fine!

Thank you.


Hi can you give the whole code ?

d_stranz
21st August 2021, 16:00
Hi can you give the whole code ?

What you do in response to a dataChanged() signal is specific to your application. Any "whole code" for this thread would probably have no meaning in your application.

I suggest you start with reading about Qt Model/View Programming (https://doc.qt.io/qt-5/model-view-programming.html) in the documentation, look at the Model/View tutorial (https://doc.qt.io/archives/qt-5.7/modelview.html), and finally look at some of the 19 examples of Model/View programming (https://doc.qt.io/archives/qt-5.7/examples-itemviews.html) that come with Qt.

You will probably learn enough in that to understand how to use the QAbstractItemModel::dataChanged() signal in your own application.