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>
{
Q_OBJECT
public:
public slots:
};
#endif
#include <QListWidgetItem>
MyDialog
::MyDialog(QWidget *parent
){
mainLayout->addWidget(staffList);
mainLayout->addWidget(result);
staffList->addItem("Fred Smith");
staffList->addItem("Billy Bloggs");
setWindowTitle(tr("Tone's Dialog"));
}
{
result->setText(item->text());
}
#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());
}
To copy to clipboard, switch view to plain text mode
Bookmarks