QTableView, QStandardItemModel and Signals

I have a QTableView populated by QStandardItemModel.
I often update the model over the network, and the model is also updated by the user directly through QTableView.

Now I like to call the method when the user changes some data, so I did:

connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(dataChanged(QStandardItem*))); 

Now the problem is that my dataChanged method is called, also when the item is updated over the network.

 model->setData(index, new_val); 

Is there another signal that is triggered only if the user changes something inside the table?

+4
source share
2 answers

No, AFAIK there is no such signal, but you can crack it.

When editing an item from QTableView an activated signal is generated. The idea is to catch this signal and connect it to the slot in which the last manually changed item will be saved.

 connect(view, SIGNAL(activated(QModelIndex), this, SLOT(manuallyActivated(QModelIndex))); void manuallyActivated(QModelIndex index) { // This variable should be in your header file... lastManuallyModifiedIndex = index; } 

Now just change your dataChanged slot to check if the item that was changed matches the last changed item.

 void dataChanged(QStandardItem* item) { // If it is invalid simply ignore it... if (lastManuallyModifiedIndex.isValid() == false) return; // only if it is modified manually we process it if (item->index() == lastManuallyModifiedIndex) { // make last modified index invalid lastManuallyModifiedIndex = QModelIndex(); doSomething(); } } 
+3
source

You can block table signals when an update arrives from your network.

 QObject::blockSignals(bool block) 

or you can listen to the click and edit the event sequentially.

+1
source

Source: https://habr.com/ru/post/1382906/


All Articles