PDA

View Full Version : Memory error reading class attribute from private slot



simonx86
4th May 2006, 19:04
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


#ifndef AIRPORTSELECTDIALOG_H
#define AIRPORTSELECTDIALOG_H

#include <qdialog.h>

using std::string;

class QComboBox;
class QStringList;
class QPushButton;
class Airport;
class QLabel;

class AirportSelectDialog : public QDialog
{
Q_OBJECT
public:
AirportSelectDialog(const QStringList airportList, QWidget *parent = 0, const char *name = 0);

signals:
void QSetAirport(const QString &str);

private slots:
void selectClicked();

private:
QLabel* label;
QComboBox *airportBox;
QPushButton *selectButton;
};

#endif


airportselectdialog.cpp - (comments added as means of conveying my problem)


#include <qcombobox.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qlayout.h>

#include "airportselectdialog.h"

AirportSelectDialog::AirportSelectDialog(const QStringList airportList, QWidget *parent, const char *name)
: QDialog(parent, name)
{
setCaption(tr("Select Airport"));

label = new QLabel(tr("Your Airport:"), this);

QComboBox *airportBox = new QComboBox(this);
airportBox->insertStringList(airportList);
label->setBuddy(airportBox);

QPushButton *selectButton = new QPushButton(tr("Select"),this);
selectButton->setDefault(true);
selectButton->setEnabled(true);
connect(selectButton, SIGNAL(clicked()),
this, SLOT(selectClicked()));

QHBoxLayout *leftLayout = new QHBoxLayout;
leftLayout->addWidget(label);
leftLayout->addWidget(airportBox);

QHBoxLayout *rightLayout = new QHBoxLayout;
rightLayout->addWidget(selectButton);

QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setMargin(11);
mainLayout->setSpacing(6);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
}

void AirportSelectDialog::selectClicked()
{
QString text = airportBox->currentText(); // This does not work!
/* I have also tried accessing selectButton from here, to no avail! */


/* Unhandled exception at 0x004014fa in Airport Booking System.exe: 0xC0000005: Access violation reading location 0xbaadf00d */

emit QSetAirport(text); // here signal will be caught by slot in seperate class
close();
}


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

jacek
4th May 2006, 19:13
AirportSelectDialog::AirportSelectDialog( ... )
{
...
QComboBox *airportBox = new QComboBox(this); // in this line you declare a new, local airportBox variable
...
}
Try:
airportBox = new QComboBox(this);

simonx86
4th May 2006, 19:17
Try:
airportBox = new QComboBox(this);

Thanks jacek, I feel like such a fool! :o

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