Results 1 to 6 of 6

Thread: Add a new row to QTableWidget with initialized cells cause to segmentation fault

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Add a new row to QTableWidget with initialized cells cause to segmentation fault

    I have this slot:
    Qt Code:
    1. void MainWindow::addRow()
    2. {
    3. int rows = ui->tableWidget->rowCount();
    4. ui->tableWidget->insertRow(rows);
    5. item = new QTableWidgetItem;
    6. item->setText(QString::number(0, 'f', 2));
    7. ui->tableWidget->setItem(rows - 1, 1, item);
    8. item = new QTableWidgetItem;
    9. item->setText(QString::number(0, 'f', 2));
    10. ui->tableWidget->setItem(rows - 1, 2, item);
    11. }
    To copy to clipboard, switch view to plain text mode 
    When I add a new row, I get a segmentation fault, why?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Add a new row to QTableWidget with initialized cells cause to segmentation fault

    Quote Originally Posted by SIFE View Post
    Qt Code:
    1. ui->tableWidget->setItem(rows - 1, 1, item);
    2. // [..]
    3. ui->tableWidget->setItem(rows - 1, 2, item);
    To copy to clipboard, switch view to plain text mode 
    Musn't that be
    Qt Code:
    1. ui->tableWidget->setItem(rows - 1, 0, item);
    2. // [..]
    3. ui->tableWidget->setItem(rows - 1, 1, item);
    To copy to clipboard, switch view to plain text mode 

    And use a debugger to see where exactly the application crashes.

  3. #3
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add a new row to QTableWidget with initialized cells cause to segmentation fault

    I tried this:

    Qt Code:
    1. item = new QTableWidgetItem;
    2. item->setText(QString::number(0, 'f', 2));
    3. ui->tableWidget->setItem(rows, 0, item);
    4. /*item = new QTableWidgetItem;
    5.   item->setText(QString::number(0, 'f', 2));
    6.   ui->tableWidget->setItem(rows, 1, item);*/
    To copy to clipboard, switch view to plain text mode 
    This make first column initialized, but I need only the second and third columns to be initialized.
    And use a debugger to see where exactly the application crashes.
    Debugger didn't work with me in FreeBSD.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Add a new row to QTableWidget with initialized cells cause to segmentation fault

    And you are using
    Qt Code:
    1. ui->tableWidget->setItem(rows, 0, item);
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. ui->tableWidget->setItem(rows-1, 0, item);
    To copy to clipboard, switch view to plain text mode 
    ? And if you don't use a debugger, what makes you sure this slot is causing the trouble? Comment all out and add it one by one to see where the crash happens.

  5. #5
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add a new row to QTableWidget with initialized cells cause to segmentation fault

    It 's appear like another slot cause the problem:
    Qt Code:
    1. //connected signals
    2. connect(ui->tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(calc(int,int)));
    3.  
    4.  
    5. void MainWindow::calc(int x,int y)
    6. {
    7. if (y == 1)
    8. {
    9. float ttl = ui->tableWidget->item(x, y)->data(0).toFloat() * ui->tableWidget->item(x, 2)->data(0).toFloat();
    10. item->setText(QString::number(ttl, 'f', 2));
    11. ui->tableWidget->setItem(x, 3, item);
    12. ttl = 0;
    13. int rows = ui->tableWidget->rowCount();
    14. for (int i = 0; i < rows; ++i)
    15. {
    16. ttl += ui->tableWidget->item(i, 3)->data(0).toFloat();
    17. }
    18.  
    19. ui->totaleEdit->setText(QString::number(ttl, 'r', 2));
    20. }
    21. else if (y == 2)
    22. {
    23. float ttl = ui->tableWidget->item(x, y)->data(0).toFloat() * ui->tableWidget->item(x, 1)->data(0).toFloat();
    24. item->setText(QString::number(ttl, 'f', 2));
    25. ui->tableWidget->setItem(x, 3, item);
    26. ttl = 0;
    27. int rows = ui->tableWidget->rowCount();
    28. for (int i = 0; i < rows; ++i)
    29. {
    30. ttl += ui->tableWidget->item(i, 3)->data(0).toFloat();
    31. }
    32.  
    33. ui->totaleEdit->setText(QString::number(ttl, 'r', 2));
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    So, I cheated with this condition:
    Qt Code:
    1. if (y == 1 && ui->tableWidget->item(x, y)->data(0).toString() == "")
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Add a new row to QTableWidget with initialized cells cause to segmentation fault

    That's why a debugger is a good choice! Your code look also hard to maintain... and
    Qt Code:
    1. ttl = 0;
    2. int rows = ui->tableWidget->rowCount();
    3. for (int i = 0; i < rows; ++i)
    4. {
    5. ttl += ui->tableWidget->item(i, 3)->data(0).toFloat();
    6. }
    7.  
    8. ui->totaleEdit->setText(QString::number(ttl, 'r', 2));
    To copy to clipboard, switch view to plain text mode 
    is madness for large tables. Better store the old value of (x,3) and calc the difference and only add/substract that from ui->totaleEdit.

Similar Threads

  1. Segmentation Fault
    By Ryan Riffle in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2011, 20:52
  2. QWT - Segmentation Fault
    By Wojtek.wk in forum Newbie
    Replies: 0
    Last Post: 17th April 2010, 14:29
  3. Segmentation Fault
    By Krish_ng in forum Qt Programming
    Replies: 8
    Last Post: 7th August 2007, 10:49
  4. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 07:33
  5. Replies: 2
    Last Post: 25th March 2006, 06:54

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.