Results 1 to 3 of 3

Thread: Segmentation Fault accessing QListWidget

  1. #1

    Default Segmentation Fault accessing QListWidget

    I am a total QT noob !!!!!

    I am receiving a SIGSEGV error when I try adding an item to a QListWidget object. The objected was created in the class constructor and is part of the constructor layout. It crashes when I try adding an item in a seperate function. I have no problems adding to the object in the constructor or adding items to an object created in the function but can not add to the constructor object from the separate function:

    Any suggestions would be appreciated especially since it may help explain a lot of other problems.

    Thanks
    ------------------------------

    The following is an extract of the code:

    Qt Code:
    1. //direct.h
    2.  
    3. #include <QPushButton>
    4. //
    5. class direct : public QDialog
    6. {
    7. Q_OBJECT
    8.  
    9.  
    10. public:
    11.  
    12.  
    13. QListWidget * listWidget ; //= new QListWidget;
    14. QPushButton * button1 ;
    15.  
    16. public slots:
    17. void getSelectedNames();
    18.  
    19. public:
    20. direct();
    21.  
    22. };
    23. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //=======================================================
    2. // S o u r c e F i l e
    3. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++
    4. #include "direct.h"
    5.  
    6. direct::direct( ) : QDialog()
    7. {
    8.  
    9. QVBoxLayout * mainLayout = new QVBoxLayout(this);
    10.  
    11. QPushButton * button1 = new QPushButton("List Files");
    12. QPushButton * quit = new QPushButton("Quit");
    13.  
    14. // mainLayout->addWidget(listWidget);
    15. mainLayout->addWidget(button1);
    16.  
    17.  
    18. // Conections
    19. connect (button1 , SIGNAL (clicked () ), this, SLOT (getSelectedNames () ) );
    20. connect (quit, SIGNAL (clicked() ), this, SLOT (accept() ) );
    21. }
    22.  
    23.  
    24.  
    25.  
    26. void direct::getSelectedNames()
    27. {
    28.  
    29.  
    30. for (int i1 =0; i1<45;i1++)
    31. {
    32. //listWidget->setCurrentRow(i1);
    33. newList->addItem("xxx"); // SEGMENTATION FAULT - but not if in the constructor
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 20th December 2008 at 12:39. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation Fault accessing QListWidget

    Add the following line in your constructor

    Qt Code:
    1. listWidget = new QListWidget(this);
    To copy to clipboard, switch view to plain text mode 

    Also, I see that the following line is not correct

    Qt Code:
    1. newList->addItem("xxx"); // SEGMENTATION FAULT - but not if in the constructor
    To copy to clipboard, switch view to plain text mode 

    There is no variable called newList

  3. #3

    Default Re: Segmentation Fault accessing QListWidget

    [QUOTE=munna;88097]Add the following line in your constructor

    Qt Code:
    1. listWidget = new QListWidget(this);
    To copy to clipboard, switch view to plain text mode 

    Including "this" did the trick. // Thanks


    There is no variable called newList
    That was just a mistake in trimming the code, I tried a lot of different things.

    Thanks again for your help!

Similar Threads

  1. Segmentation Faul using addItem (QListWidget)
    By gnusar in forum Qt Programming
    Replies: 3
    Last Post: 8th November 2008, 09:27
  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.