4

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

Now I like to call a method when the user is changing some data, so i did:

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

The Problem now 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 which is only emitted if, the user is changing something inside the QTableview ???

AAEM
  • 1,837
  • 2
  • 18
  • 26
nfo
  • 637
  • 2
  • 6
  • 19

2 Answers2

3

No, AFAIK there is no such signal but you there is a way to hack it.

When editing an item from the QTableView the activated signal will have been emited. The idea is to catch this signal and connect it to a slot that will store the last manually changed item.

connect(view, SIGNAL(activated(QModelIndex), this, SLOT(manuallyActivated(QModelIndex)));

void manuallyActivated(QModelIndex index)
{
   // This variable should be in your header file...
   lastManuallyModifiedIndex = index;
}

Now simply modify your dataChanged slot to check if the item that changed corresponds to the last modified 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();
    }   
}
pnezis
  • 12,023
  • 2
  • 38
  • 38
  • mhm looks like a nice workaround for my problem. after testing it, it looks like that the acivated signal is emited after selecting the item and hiting enter, but not by inline editing – nfo Nov 24 '11 at 14:55
  • i will keep ur answere, because its a accurate soloution, even the activate signal is not emited in my case (seams like its platform dependent) i use now the entered signal and activated the mousetracking, wich is may not the best soloution, but it fit my needs) – nfo Nov 24 '11 at 15:12
1

You could block the table signals when an update comes in from your network.

QObject::blockSignals(bool block)

or you could listen for click and edit event in succession.

Zaid
  • 678
  • 6
  • 13