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
Code:
#ifndef AIRPORTSELECTDIALOG_H
#define AIRPORTSELECTDIALOG_H
#include <qdialog.h>
using std::string;
class Airport;
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:
};
#endif
airportselectdialog.cpp - (comments added as means of conveying my problem)
Code:
#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
){
setCaption(tr("Select Airport"));
label
= new QLabel(tr
("Your Airport:"),
this);
airportBox->insertStringList(airportList);
label->setBuddy(airportBox);
selectButton->setDefault(true);
selectButton->setEnabled(true);
connect(selectButton, SIGNAL(clicked()),
this, SLOT(selectClicked()));
leftLayout->addWidget(label);
leftLayout->addWidget(airportBox);
rightLayout->addWidget(selectButton);
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.
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:
Re: Memory error reading class attribute from private slot
Quote:
Originally Posted by jacek
Thanks jacek, I feel like such a fool! :o
I appreciate you looking at it, and taking the time to respond.