Results 1 to 5 of 5

Thread: Segmentation Fault

  1. #1
    Join Date
    Dec 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Segmentation Fault

    I am parsing every topLevelItem of QTreeWidget and adding it's text to a QList. But I am getting a segmentation fault when I try to add the text to the list.

    Here is my code:
    Qt Code:
    1. QList<QString> ulist;
    2.  
    3. for(int x=0; x<ui->treeWidget->topLevelItemCount(); x++)
    4. {
    5. item = ui->treeWidget->topLevelItem(x-1);
    6. ulist.append(item->text(0));
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance to anyone that may be able to help.
    -Ryan

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Segmentation Fault

    Is ui->treeWidget->topLevelItem(-1) a valid index?

    Also, you do not seem to check if the item is valid before adding it to the list

  3. #3
    Join Date
    Dec 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault

    Thanks for your reply,

    ui->treeWidget->topLevelItem(x-1) is a valid index because it starts with the first item on the list, so the loop wouldn't have started unless there was atleast one item.

    But how do you check if the item is valid?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Segmentation Fault

    Yes, and x starts at zero, so zero - 1 is -1.

    So is ui->treeWidget->topLevelItem(-1) a valid index?

    Secondly, the above function will return NULL if it's invalid, and if you try to dereference a NULL pointer, well, you can guess what happens.

  5. #5
    Join Date
    Dec 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault

    Thank you very much squidge.
    I guess i didn't totally take in or understand what you posted the first time.

    Here is my new code that runs fine.
    Qt Code:
    1. QList<QString> ulist;
    2.  
    3. for(int x=0; x<ui->treeWidget->topLevelItemCount(); x++)
    4. {
    5. item = ui->treeWidget->topLevelItem(x);
    6.  
    7. if(item->text(0) != "")
    8. {
    9. ulist.append(item->text(0));
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    I can't believe i never noticed that.

    Thanks again.

Similar Threads

  1. Segmentation fault
    By Schimanski in forum Qt Programming
    Replies: 20
    Last Post: 31st August 2009, 16:26
  2. Segmentation fault
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2008, 18:59
  3. Segmentation fault
    By MarkoSan in forum Qt Programming
    Replies: 23
    Last Post: 19th October 2008, 22:40
  4. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  5. Segmentation Fault
    By merry in forum General Programming
    Replies: 4
    Last Post: 12th March 2007, 04:08

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.