Results 1 to 3 of 3

Thread: segmentation fault after QTableWidget::sortItems

  1. #1
    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 segmentation fault after QTableWidget::sortItems

    Hi,

    I fill a QTableWidget in scope:
    Qt Code:
    1. {
    2. [...]
    3. wdg_check *wdgl_check = new wdg_check();
    4. ui.languages->setCellWidget( currentRow, 2, wdgl_check );
    5. QObject::connect( ui.languages->cellWidget(currentRow,2), SIGNAL(newSelection()), this, SLOT(page1_selection_change()) );
    6. [...]
    7. QTableWidgetItem *newItemC = new QTableWidgetItem( i.value()["ol"]+"-"+i.value()["fl"] );
    8. ui.languages->setItem( currentRow, 6, newItemC);
    9. }
    10. // ui.languages->sortItems( 6, Qt::AscendingOrder );
    To copy to clipboard, switch view to plain text mode 

    where ui.languages is a QTableWidget and i.value() a QMap<QString, QString>. wdg_check is a simple Widget with a QCheckbox, which emit the SIGNAL newSelection(), when the state of QCheckbox is changed. And wdg_check provides the function getStatus(), which returns wether the QCheckBox is checked or not (bool).

    so I have defined the connection-slot as:
    Qt Code:
    1. void dgl_newBin::page1_selection_change()
    2. {
    3. for (int i = 0; i < ui.languages->rowCount(); ++i)
    4. {
    5. bool state = static_cast<wdg_check*>(ui.languages->cellWidget(i,2))->getStatus();
    6. }
    7. // (I have all stuf deleted, which doesn't cause the Problem)
    8. }
    To copy to clipboard, switch view to plain text mode 

    Ok, if I run the application with that code, all works, but if I uncomment the line "ui.languages->sortItems( 6, Qt::AscendingOrder );" I get an segmentation fault at the line "bool state = static_cast<wdg_check*>(ui.languages->cellWidget(i,2))->getStatus();".

    Why? Isn't ui.languages->cellWidget(i,2) valid after sorting?

    Thanks,
    Lykurg

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: segmentation fault after QTableWidget::sortItems

    Quote Originally Posted by Lykurg
    wdg_check is a simple Widget with a QCheckbox, which emit the SIGNAL newSelection(), when the state of QCheckbox is changed. And wdg_check provides the function getStatus(), which returns wether the QCheckBox is checked or not (bool).
    If checkbox behaviour is all you need, you could simply use:
    QTableWidgetItem::setCheckState()
    [Edit] All items have a "checkable" flag by default, but as I recall, in case you want a checkbox to appear, you need to initialize those cells to a certain check state with the above method.

    Quote Originally Posted by Lykurg
    I get an segmentation fault at the line "bool state = static_cast<wdg_check*>(ui.languages->cellWidget(i,2))->getStatus();".
    Apparently the cast fails. You should always check the pointer after casting.
    I'm not sure about the sorting problem, though..
    Last edited by jpn; 22nd March 2006 at 19:38.

  3. #3
    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: segmentation fault after QTableWidget::sortItems

    Quote Originally Posted by jpn
    Ok, than I take the easy way :-) (and now I am able to sort)

    Don't why I was geting the segmentation fault, but it dosn't matter anymore, because QTableWidgetItem::setCheckState() works just fine.

    Thanks

Similar Threads

  1. segmentation fault for no apparent reason
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 12th February 2009, 11:13
  2. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  3. Process aborted. Segmentation fault
    By Pragya in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2007, 08:12
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

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.