PDA

View Full Version : Segmentation Faul using addItem (QListWidget)



gnusar
7th November 2008, 22:59
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.


class PlayerWidget : public QWidget
{
Q_OBJECT

public:
PlayerWidget(QWidget *parent = 0);

public slots:
void newPlayer(QString);

private:
QLabel *playerLabel;
QListWidget *playerView;

short numplayer;
};



PlayerWidget::PlayerWidget(QWidget *parent)
: QWidget(parent)
{
QLabel *playerLabel = new QLabel(tr("Players:"));
QListWidget *playerView = new QListWidget();

QVBoxLayout *playerLayout = new QVBoxLayout;
playerLayout->addWidget(playerLabel);
playerLayout->addWidget(playerView);

setLayout(playerLayout);

numplayer = 0;
playerView->addItem(QString("mooooooooo")); // it works here!!!
}

void PlayerWidget::newPlayer(QString name)
{
//playerView->addItem(QString("name")); // not here
new QListWidgetItem(tr("mooo"), playerView); // nor here!

numplayer++;
}

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

h123
8th November 2008, 04:10
You could try with
QListWidgetItem ( QString("Woooo", playerView);

also how are you connecting the slots ?

spirit
8th November 2008, 07:38
the mistake in ctor


QLabel *playerLabel = new QLabel(tr("Players:"));
QListWidget *playerView = new QListWidget();


must be


playerLabel = new QLabel(tr("Players:"));
playerView = new QListWidget();

becuase you already have playerLabel, playerView as class member variables.

gnusar
8th November 2008, 10:27
thank you spirit, it runs perfectly fine now!