PDA

View Full Version : ListWidget itemClicked problem



Keemosabi
10th August 2008, 23:58
I have just started learning Qt and have tried to write a simple little application which has a ListWidget and a LineEdit. When the user clicks an item in the ListWidget the text should appear in the LineEdit. It compiles and runs but when the item is clicked a segmentation fault occurs. I have searched through many examples but can't find one which is close enough to what I am trying to do to help. I would be grateful if someone could assist by pointing out where I have gone wrong. It's probably something very basic.


#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>
#include <QListWidgetItem>

class QLineEdit;

class MyDialog : public QDialog
{
Q_OBJECT

public:
MyDialog(QWidget *parent = 0);
QLineEdit *result;

public slots:
void nameToLE(QListWidgetItem *item);

};

#endif

#include <QListWidgetItem>

MyDialog::MyDialog(QWidget *parent)
: QDialog(parent)
{
QVBoxLayout* mainLayout = new QVBoxLayout(this);
QListWidget* staffList = new QListWidget();
QLineEdit* result = new QLineEdit();

mainLayout->addWidget(staffList);
mainLayout->addWidget(result);

staffList->addItem("Fred Smith");
staffList->addItem("Billy Bloggs");

setWindowTitle(tr("Tone's Dialog"));

connect(staffList, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(nameToLE(QListWidgetItem*)));
}

void MyDialog::nameToLE(QListWidgetItem *item)
{
result->setText(item->text());
}

jacek
11th August 2008, 01:10
You have two result variables. One is a member of MyDialog, the second is a local variable in MyDialog constructor.

Keemosabi
11th August 2008, 23:29
Thanks for your help Jacek.

I changed line31 from
QLineEdit* result = new QLineEdit(); to
result = new QLineEdit; and it now works.