Results 1 to 3 of 3

Thread: ListWidget itemClicked problem

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

    Default ListWidget itemClicked problem

    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.

    Qt Code:
    1. #ifndef MYDIALOG_H
    2. #define MYDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QListWidgetItem>
    6.  
    7. class QLineEdit;
    8.  
    9. class MyDialog : public QDialog
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyDialog(QWidget *parent = 0);
    15. QLineEdit *result;
    16.  
    17. public slots:
    18. void nameToLE(QListWidgetItem *item);
    19.  
    20. };
    21.  
    22. #endif
    23.  
    24. #include <QListWidgetItem>
    25.  
    26. MyDialog::MyDialog(QWidget *parent)
    27. : QDialog(parent)
    28. {
    29. QVBoxLayout* mainLayout = new QVBoxLayout(this);
    30. QListWidget* staffList = new QListWidget();
    31. QLineEdit* result = new QLineEdit();
    32.  
    33. mainLayout->addWidget(staffList);
    34. mainLayout->addWidget(result);
    35.  
    36. staffList->addItem("Fred Smith");
    37. staffList->addItem("Billy Bloggs");
    38.  
    39. setWindowTitle(tr("Tone's Dialog"));
    40.  
    41. connect(staffList, SIGNAL(itemClicked(QListWidgetItem*)),
    42. this, SLOT(nameToLE(QListWidgetItem*)));
    43. }
    44.  
    45. void MyDialog::nameToLE(QListWidgetItem *item)
    46. {
    47. result->setText(item->text());
    48. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 11th August 2008 at 20:40. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ListWidget itemClicked problem

    You have two result variables. One is a member of MyDialog, the second is a local variable in MyDialog constructor.

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

    Default Re: ListWidget itemClicked problem

    Thanks for your help Jacek.

    I changed line31 from
    Qt Code:
    1. QLineEdit* result = new QLineEdit();
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. result = new QLineEdit;
    To copy to clipboard, switch view to plain text mode 
    and it now works.

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. ListWidget horizontal scroll.
    By patrick772goh in forum Qt Tools
    Replies: 3
    Last Post: 17th July 2007, 07:32
  4. Replies: 16
    Last Post: 7th March 2006, 15:57

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.