Results 1 to 4 of 4

Thread: Segmentation Faul using addItem (QListWidget)

  1. #1

    Default Segmentation Faul using addItem (QListWidget)

    Hey there,
    I am having trouble with QListWidget.. When I try to add an Item to it, a segmentation fault occurs..
    Hope you guys can help me out.

    Qt Code:
    1. class PlayerWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PlayerWidget(QWidget *parent = 0);
    7.  
    8. public slots:
    9. void newPlayer(QString);
    10.  
    11. private:
    12. QLabel *playerLabel;
    13. QListWidget *playerView;
    14.  
    15. short numplayer;
    16. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. PlayerWidget::PlayerWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QLabel *playerLabel = new QLabel(tr("Players:"));
    5. QListWidget *playerView = new QListWidget();
    6.  
    7. QVBoxLayout *playerLayout = new QVBoxLayout;
    8. playerLayout->addWidget(playerLabel);
    9. playerLayout->addWidget(playerView);
    10.  
    11. setLayout(playerLayout);
    12.  
    13. numplayer = 0;
    14. playerView->addItem(QString("mooooooooo")); // it works here!!!
    15. }
    16.  
    17. void PlayerWidget::newPlayer(QString name)
    18. {
    19. //playerView->addItem(QString("name")); // not here
    20. new QListWidgetItem(tr("mooo"), playerView); // nor here!
    21.  
    22. numplayer++;
    23. }
    To copy to clipboard, switch view to plain text mode 

    newPlayer(QString) is a slot that is called by another widgets signal

  2. #2
    Join Date
    Aug 2008
    Posts
    60
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation Faul using addItem (QListWidget)

    You could try with
    QListWidgetItem ( QString("Woooo", playerView);

    also how are you connecting the slots ?

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Segmentation Faul using addItem (QListWidget)

    the mistake in ctor
    Qt Code:
    1. QLabel *playerLabel = new QLabel(tr("Players:"));
    2. QListWidget *playerView = new QListWidget();
    To copy to clipboard, switch view to plain text mode 

    must be
    Qt Code:
    1. playerLabel = new QLabel(tr("Players:"));
    2. playerView = new QListWidget();
    To copy to clipboard, switch view to plain text mode 
    becuase you already have playerLabel, playerView as class member variables.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4

    Default Re: Segmentation Faul using addItem (QListWidget)

    thank you spirit, it runs perfectly fine now!

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.