QTableWidget ondatachange event disconnect
-
Hi,
Actually i have a trouble that i can't solve and i couldn't found any solution at the moment...I have a QTablewidget with two columns that i want to fill with two different vectors. When i want to change the table's content with another different two vectors, first i call clearcontents() to set empty every cells, and after i fill the table with the new vectors.
The problem is that if i clear the contents from the table, when i fill the table the program crashes and says "segmentation fault".
After debug i saw that each time i fill one cell the program jump to the event on_datachanged, where i have a few instructions that can be the reason of the crash.
So basically i don't know how to disconnect this event to avoid this problem. Anyway, maybe there's an easier way to clear and fill again a qtablewidget that at the end, is my simple objective.
Any suggestion?
Thanks,
avmg. -
Hi,
First there is the disconnect function to not call the slot, but that is a bit hard. When you only need to suppress the signals for a couple of instructions the @bool QObject::blockSignals(bool block)@ function (QObject derived function) may be used. Or implement the eventFilter and when the cell are filled, call the slot, if cells are empty, simply ignore the event. -
Hi again,
Nothing....I tried:
blockSignals(true);
....
....
....
blockSignals(false);Also:
bool oldState = ui->table->blockSignals(true);
...
...
ui->table->blockSignals(oldState);But the ondatachanged event still triggering. Also i'm not sure how to fill the cell content because even the instruction executes the value doesn't appear on the table. I tried:
// Option 1:
for(int i=0;i<10;i++)
{
ui->Table->item(i,0)->setData(Qt::DisplayRole, *(V+i));
}// Option 2:
for(int i=0;i<10;i++)
{
ui->Table->item(i,0)->setText(QString::number(*(V+i)));
}// Option 3:
for(int i=0;i<10;i++)
{
ui->Table->setItem(i,0,new QTableWidgetItem(V[i]));
}V is a pointer to a double type vector. With this trigger signal and the filling table i'm getting a bit lost... I don't know why something as easy like clear and fill a table with double values is being so complicated for me...
Thanks
-
Hi,
You are blocking the signals from the view which is not what you want, it's the model that emits the signals you are after. BUT, you should rather fix your logic rather than work around the system.
Option 3 corrected:
@for(int i=0;i<10;i++)
{ ui->Table->setItem(i,0,new QTableWidgetItem(QString::number(V[i])));
}@ -
If you want to fix the logic, the QEventFilter may be used. Or in the slot make sure the sells do have data items in it before executing the code!
-
Solved!
Finally my problem was the way i was filling the table, i changed it as SGaist said and it's working. Otherwise, can you show me and example of using the QEventFilter?
Thanks!
-
Here is the doc
"Events and Filters doc":http://qt-project.org/doc/qt-4.8/eventsandfilters.html -
Thanks, this is very helpfull !