Results 1 to 5 of 5

Thread: Infinite loop upon trying to modify item in the QTableWidget

  1. #1
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Infinite loop upon trying to modify item in the QTableWidget

    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.
    Qt Code:
    1. void MainWindow::onEditView(QTableWidgetItem* item){
    2. QString changed_t = ui->tableWidget->currentItem()->text();
    3.  
    4. //Obtaining the row and column values for the edited item
    5. int r_id= changed_t->row();
    6. int c_id=changed_t->column();
    7. int line_count=0;
    8.  
    9. //Changing the particular value in the file
    10. QFile file_r("address1.txt");
    11.  
    12. if(!file_r.exists()){
    13. return;
    14. }
    15.  
    16. QString line_app;
    17. QTextStream wstream(&file_r);
    18.  
    19. if(file_r.open(QIODevice::ReadWrite)){
    20.  
    21. while(!file_r.atEnd()){
    22. QStringList line_data;
    23. QString line=file_r.readLine();
    24. line=line.simplified();
    25. line_data=line.split(";",QString::SkipEmptyParts);
    26.  
    27. for(int j=0;j<line_data.size();j++){
    28.  
    29. if((j==c_id)&&(line_count==r_id)){
    30. line_data.replace(j,changed_t);
    31. qDebug()<<"split: "<<line_data;
    32. line.clear();
    33. if(j==line_data.size()-1){
    34. line_app.append(line_data.at(j)+"\n");
    35. }
    36. else{
    37. line_app.append(line_data.at(j)+";");
    38. }
    39. }
    40. ...........
    To copy to clipboard, switch view to plain text mode 

    Any advice on the above?

  2. #2
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Infinite loop upon trying to modify item in the QTableWidget

    Are you changing an "item"?
    Show whole MainWindow:nEditView().

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Infinite loop upon trying to modify item in the QTableWidget

    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.

  4. #4
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: Infinite loop upon trying to modify item in the QTableWidget

    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
    Qt Code:
    1. void MainWindow::onEditView()
    2. {//Capturing the changed value from the table
    3. QString changed_t=ui->tableWidget->currentItem()->text();
    4. qDebug()<<changed_t<<__FUNCTION__;
    5.  
    6. //Obtaining the row and column values for the edited item
    7. int r_id=ui->tableWidget->currentItem()->row();
    8. int c_id=ui->tableWidget->currentItem()->column();
    9. int line_count=0;
    10.  
    11. //Changing the particular value in the file
    12. QFile file_r("address1.txt");
    13. if(!file_r.exists()){
    14. return;
    15. }
    16.  
    17. QString line_app;
    18. QTextStream wstream(&file_r);
    19.  
    20. if(file_r.open(QIODevice::ReadWrite)){
    21. while(!file_r.atEnd()){
    22. QStringList line_data;
    23. QString line=file_r.readLine();
    24. line=line.simplified();
    25. line_data=line.split(";",QString::SkipEmptyParts);
    26.  
    27. for(int j=0;j<line_data.size();j++){
    28. if((j==c_id)&&(line_count==r_id)){
    29. line_data.replace(j,changed_t);
    30. //qDebug()<<"split: "<<line_data;
    31. line.clear();
    32. if(j==line_data.size()-1){
    33. line_app.append(line_data.at(j)+"\n");
    34. }
    35. else{
    36. line_app.append(line_data.at(j)+";");
    37. }
    38. }
    39. else{
    40. if(j==line_data.size()-1){
    41. line_app.append(line_data.at(j)+"\n");
    42. }
    43. else{
    44. line_app.append(line_data.at(j)+";");
    45. }
    46. }
    47. }
    48. //qDebug()<<line;
    49. line_count++;
    50. }
    51. file_r.resize(0);
    52. wstream<<line_app;
    53. file_r.close();
    54. }
    55.  
    56. }
    To copy to clipboard, switch view to plain text mode 

    Any advice please

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Infinite loop upon trying to modify item in the QTableWidget

    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,
    _

Similar Threads

  1. Replies: 1
    Last Post: 28th February 2014, 09:39
  2. Replies: 6
    Last Post: 3rd August 2012, 07:48
  3. QThread : how to stop an infinite loop
    By TarielVincent in forum Qt Programming
    Replies: 9
    Last Post: 24th February 2012, 22:22
  4. Infinite loop in QXmlSchemaValidator::validate()?
    By TropicalPenguin in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2010, 16:09
  5. infinite loop
    By zakis in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2009, 18:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.