When will cellChanged signal be fired in QTableWidget?
I am using a QTableWidget to display and edit a data matrix. For validation purpose, I used the QLineEdit as items in this table. As following,
pTable=new QTableWidget(N,N,this);
pItem=new QLineEdit();
pItem->setText(tr("%1").arg(pInfra->adjacencyM(i,j)));
rx=new QRegExp("0|1");
validatorRegexp=new QRegExpValidator(*rx,0);
pItem->setValidator(validatorRegexp);
pTable->setCellWidget(i,j,pItem);
Since I want to know if data in certain cell has been changed, so I tried cellChanged(int, int) signal, and connect it with my own slot cellEdited(int,int), like this
开发者_StackOverflow社区connect(pTable,SIGNAL(cellChanged(int,int)),this, SLOT(cellEdited(int,int)));
But, when I edit QLineEdit in the cell, I can not catch this signal. When will this signal be fired? Or can I do this using another signal or in some other way? Thanks!
The problem is that the cellChanged()
signal is emittet only if the table model is issued the setData()
method, which normally comes from the QLineEdit
of the delegate. Since You have your own mechanism by setting the cell widget the setData()
method of the model will never be called. Which means you'll have to connect to the textChanged()
or the textEdited()
signal of the QLineEdit
object you put in the cells.
Another valid option is the approach mentioned by beduin in the comment.
Also possible: You could subclass the used delegate and make it create QLineEdit
objects with your validator. Which would be the cleanest approach since you don't interfere with the model/view architecture and can rely on the signals the table object is sending.
Best regards D
Not aware about causes of this problem. Considering another ways. You can catch QLineEdit
signal textChanged
and use QSignalMapper to bind signal, fired by each QLineEdit
to particular cell number. Maybe itsn't the best qway to do that, but you can use it in case this problem won't be solved.
精彩评论