PDA

View Full Version : Infinite loop upon trying to modify item in the QTableWidget



newtoQ_s
20th October 2015, 06:47
Hi,

I am getting an infinite loop while trying to modify an item on the tableWidget and subsequently in the corresponding file as well and display the same onto the screen using the below signal and slot mechanism. I have also tried to use other signals to trigger the same but it results in the same loop.
I have tried triggering the signal from a different function and from the constructor but both result in the same.

connect(ui->tableWidget,SIGNAL(itemChanged(QTableWidgetItem*)) ,this,SLOT(onEditView(QTableWidgetItem*)));


The following is the user defined slot.


void MainWindow::onEditView(QTableWidgetItem* item){
QString changed_t = ui->tableWidget->currentItem()->text();

//Obtaining the row and column values for the edited item
int r_id= changed_t->row();
int c_id=changed_t->column();
int line_count=0;

//Changing the particular value in the file
QFile file_r("address1.txt");

if(!file_r.exists()){
return;
}

QString line_app;
QTextStream wstream(&file_r);

if(file_r.open(QIODevice::ReadWrite)){

while(!file_r.atEnd()){
QStringList line_data;
QString line=file_r.readLine();
line=line.simplified();
line_data=line.split(";",QString::SkipEmptyParts);

for(int j=0;j<line_data.size();j++){

if((j==c_id)&&(line_count==r_id)){
line_data.replace(j,changed_t);
qDebug()<<"split: "<<line_data;
line.clear();
if(j==line_data.size()-1){
line_app.append(line_data.at(j)+"\n");
}
else{
line_app.append(line_data.at(j)+";");
}
}
...........

Any advice on the above?

west
23rd October 2015, 19:55
Are you changing an "item"?
Show whole MainWindow::onEditView().

ChrisW67
23rd October 2015, 20:15
The code presented will not compile. Changed_t is a QString (line 2) that is neither a pointer type nor has a row() or column() function (lines 5/6).

Aside from that, the assumption at line 2 that the item that changed must be the currentItem() is flawed. The item that changed is passed directly into the slot function... use that pointer.

newtoQ_s
26th October 2015, 10:28
Hi,

Thanks for the responses. I did some debugging and have been able to avoid the infinite loop however, I have run into the below mentioned issues.
\ I have found that there is a signal called cellchanged() that emits changes made onto the table to a custom slot called OnEditView().

This function looks for the changed item using the code above.

The issue is that I do not want this signal to be emitted upon adding new rows to the tableWidget. If I delete a row and add a new one, I do not get the following error; it occurs only upon trying to add a new row to the table.
Exception at 0x657aa87c, code:0xc0000005:read access violation at:0x0, flags=0x0 (first chance)

However, this signal is meant to be emitted only when an existing item is modified, but it seems to be emitted upon trying to add a new row as well. That is where the problem is.
I used a similar signal called itemchanged() then too I face the same issue.
But the same signal when used with QTAbleView seems to work fine.

The following is EditView


void MainWindow::onEditView()
{//Capturing the changed value from the table
QString changed_t=ui->tableWidget->currentItem()->text();
qDebug()<<changed_t<<__FUNCTION__;

//Obtaining the row and column values for the edited item
int r_id=ui->tableWidget->currentItem()->row();
int c_id=ui->tableWidget->currentItem()->column();
int line_count=0;

//Changing the particular value in the file
QFile file_r("address1.txt");
if(!file_r.exists()){
return;
}

QString line_app;
QTextStream wstream(&file_r);

if(file_r.open(QIODevice::ReadWrite)){
while(!file_r.atEnd()){
QStringList line_data;
QString line=file_r.readLine();
line=line.simplified();
line_data=line.split(";",QString::SkipEmptyParts);

for(int j=0;j<line_data.size();j++){
if((j==c_id)&&(line_count==r_id)){
line_data.replace(j,changed_t);
//qDebug()<<"split: "<<line_data;
line.clear();
if(j==line_data.size()-1){
line_app.append(line_data.at(j)+"\n");
}
else{
line_app.append(line_data.at(j)+";");
}
}
else{
if(j==line_data.size()-1){
line_app.append(line_data.at(j)+"\n");
}
else{
line_app.append(line_data.at(j)+";");
}
}
}
//qDebug()<<line;
line_count++;
}
file_r.resize(0);
wstream<<line_app;
file_r.close();
}

}

Any advice please

anda_skoa
26th October 2015, 11:06
The error suggests that you are trying to access a null pointer.
From a quick glance it seems you are not checking the return value of QTableWidget::currentItem() before accessing it.

Cheers,
_