Results 1 to 3 of 3

Thread: Memory error reading class attribute from private slot

  1. #1
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    2
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Memory error reading class attribute from private slot

    I've been trying to find a solution to this on my own this past week, but have been confounded every step of the way.

    I am using MSVS.NET(2003) and QT 3.2.1 Non-Commercial.

    I coded a simple dialog which accepts a QStringList as an argument in the constructor, and this list is displayed in a QComboBox (which is a private attribute of the class). I wish to use an instance of QPushButton to pass the value of the selected variable to another class using emit. However, after the slot method which deals with this processing is called when the push button recieves a clicked() signal, I cannot access the QComboBox and thus obtain its current value using the methods.

    airportselectdialog.h
    Qt Code:
    1. #ifndef AIRPORTSELECTDIALOG_H
    2. #define AIRPORTSELECTDIALOG_H
    3.  
    4. #include <qdialog.h>
    5.  
    6. using std::string;
    7.  
    8. class QComboBox;
    9. class Airport;
    10. class QLabel;
    11.  
    12. class AirportSelectDialog : public QDialog
    13. {
    14. Q_OBJECT
    15. public:
    16. AirportSelectDialog(const QStringList airportList, QWidget *parent = 0, const char *name = 0);
    17.  
    18. signals:
    19. void QSetAirport(const QString &str);
    20.  
    21. private slots:
    22. void selectClicked();
    23.  
    24. private:
    25. QLabel* label;
    26. QComboBox *airportBox;
    27. QPushButton *selectButton;
    28. };
    29.  
    30. #endif
    To copy to clipboard, switch view to plain text mode 

    airportselectdialog.cpp - (comments added as means of conveying my problem)
    Qt Code:
    1. #include <qcombobox.h>
    2. #include <qpushbutton.h>
    3. #include <qlabel.h>
    4. #include <qlayout.h>
    5.  
    6. #include "airportselectdialog.h"
    7.  
    8. AirportSelectDialog::AirportSelectDialog(const QStringList airportList, QWidget *parent, const char *name)
    9. : QDialog(parent, name)
    10. {
    11. setCaption(tr("Select Airport"));
    12.  
    13. label = new QLabel(tr("Your Airport:"), this);
    14.  
    15. QComboBox *airportBox = new QComboBox(this);
    16. airportBox->insertStringList(airportList);
    17. label->setBuddy(airportBox);
    18.  
    19. QPushButton *selectButton = new QPushButton(tr("Select"),this);
    20. selectButton->setDefault(true);
    21. selectButton->setEnabled(true);
    22. connect(selectButton, SIGNAL(clicked()),
    23. this, SLOT(selectClicked()));
    24.  
    25. QHBoxLayout *leftLayout = new QHBoxLayout;
    26. leftLayout->addWidget(label);
    27. leftLayout->addWidget(airportBox);
    28.  
    29. QHBoxLayout *rightLayout = new QHBoxLayout;
    30. rightLayout->addWidget(selectButton);
    31.  
    32. QHBoxLayout *mainLayout = new QHBoxLayout(this);
    33. mainLayout->setMargin(11);
    34. mainLayout->setSpacing(6);
    35. mainLayout->addLayout(leftLayout);
    36. mainLayout->addLayout(rightLayout);
    37. }
    38.  
    39. void AirportSelectDialog::selectClicked()
    40. {
    41. QString text = airportBox->currentText(); // This does not work!
    42. /* I have also tried accessing selectButton from here, to no avail! */
    43.  
    44.  
    45. /* Unhandled exception at 0x004014fa in Airport Booking System.exe: 0xC0000005: Access violation reading location 0xbaadf00d */
    46.  
    47. emit QSetAirport(text); // here signal will be caught by slot in seperate class
    48. close();
    49. }
    To copy to clipboard, switch view to plain text mode 

    There are no errors/warnings during compilation, and the dialog is created as a popup window which will close once item selected from QComboBox.

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

    Default Re: Memory error reading class attribute from private slot

    Quote Originally Posted by simonx86
    AirportSelectDialog::AirportSelectDialog( ... )
    {
    ...
    QComboBox *airportBox = new QComboBox(this); // in this line you declare a new, local airportBox variable
    ...
    }
    Try:
    Qt Code:
    1. airportBox = new QComboBox(this);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    simonx86 (4th May 2006)

  4. #3
    Join Date
    May 2006
    Location
    Bristol, UK
    Posts
    2
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Memory error reading class attribute from private slot

    Quote Originally Posted by jacek
    Try:
    Qt Code:
    1. airportBox = new QComboBox(this);
    To copy to clipboard, switch view to plain text mode 
    Thanks jacek, I feel like such a fool!

    I appreciate you looking at it, and taking the time to respond.

Similar Threads

  1. Replies: 12
    Last Post: 18th September 2008, 15:04

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.