Results 1 to 2 of 2

Thread: QTableWidget::clearContents() crashes if called after selecting items

  1. #1
    Join Date
    Nov 2009
    Qt products

    Red face QTableWidget::clearContents() crashes if called after selecting items

    I "hacked" the qt4/demos/spreadsheet example and put together a program that loads a specific form of a table from file. I got rid of all editing support, and use merely the functionality for highlighting by clicking with the mouse on items in the table. I use this as selection mechanism, and then have other functions read the selected items to do other processing. All of this works nicely, except when I then try to remove the currently loaded table and replace it by another one. It crashes when I call clearContents(), either directly or by emmiting a signal that is connected to the slot clearContents() in the existing base QTableWidget (inherited from some other Qt class).

    This problem only occurs if I have actually selected an item in the table.

    Using a file selection dialogue, I can load one table from file and display it, then load another one and display and so on. For each new load, clearContents() is called and then the new table is loaded. This works, but only if I never touch the table, never bring any of the items to highlight. Otherwise, as soon as clearContents() is called, the application crashes with segmentation fault.

    Most likely I made a mistake somewhere but the question is where too look?

  2. #2
    Join Date
    Nov 2009
    Qt products

    Default Re: QTableWidget::clearContents() crashes if called after selecting items

    I guess I figured it out why this crashed: The callback updateStatus which was implemented as partially shown below, was still accessing items if they didn't even exist:
    Qt Code:
    1. void SpreadSheet::updateStatus(QTableWidgetItem *item)
    2. {
    3. if (item && item == currentItem()) {
    4. lastColumn = currentColumn();
    5. lastLine = currentRow();
    6. validSelection=true; // <----- prevents access of garbage later
    7. if (lastColumn == SPCOL::lesseme || lastColumn == SPCOL::promptname) {
    8. emit customSignalEdit( item->data(Qt::StatusTipRole).toString() );
    9. }
    10. }
    12. if (!item || !validSelection) return; // <--- that fixed it
    13. ....
    14. // remark: lastColumn and lastLine, as well as validSelection are protected variables in this class, which is derived from QTableWidget ()
    To copy to clipboard, switch view to plain text mode 
    Initially, calling the above with *item == NULL was not properly checked. It turned out that when a new table was displayed updateStatus was also called, even though nothing was selected.
    However, just below the code shown here, I accessed the pointer *item without further checking. In that code the assumption was made that previously something had been selected. But that could not be the case for a new table, because I had forgotten to initialize properly some other variables when a new table was loaded.

    At any rate, all's working now - you may move to other problems :-) - thanks to anyone who may have wasted any thoughts on this

Similar Threads

  1. Designer crashes when selecting some widgets
    By gwendal in forum Qt Tools
    Replies: 4
    Last Post: 21st July 2006, 14:18
  2. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 13:20

Tags for this Thread


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.