Hi!
When I try to call QComboBox::addItem from another function in my class than the constructor, my application crashes.
I've made a short application to reproduce the crash, here's the code (.zip included for those who want).
ComboBoxCrash.pro
HEADERS += GUI.h
SOURCES += GUI.cpp \
main.cpp
HEADERS += GUI.h
SOURCES += GUI.cpp \
main.cpp
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtCore/QCoreApplication>
#include <QtGui>
#include "GUI.h"
int main(int argc, char *argv[]){
ComboBoxCrash *MainWindow = new ComboBoxCrash;
MainWindow->show();
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QtGui>
#include "GUI.h"
int main(int argc, char *argv[]){
QApplication a(argc, argv);
ComboBoxCrash *MainWindow = new ComboBoxCrash;
MainWindow->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
GUI.h
#ifndef GUI_H
#define GUI_H
#include <QWidget>
class ComboBoxCrash
: public QWidget{ Q_OBJECT
public:
public slots:
void function();
private:
};
#endif // GUI_H
#ifndef GUI_H
#define GUI_H
#include <QWidget>
class QComboBox;
class QPushButton;
class ComboBoxCrash : public QWidget{
Q_OBJECT
public:
ComboBoxCrash(QWidget *parent=0);
public slots:
void function();
private:
QComboBox *comboBox;
QPushButton *crashButton;
};
#endif // GUI_H
To copy to clipboard, switch view to plain text mode
GUI.cpp
#include <QtGui>
#include "GUI.h"
mainLayout->addWidget(comboBox,0,0);
mainLayout->addWidget(crashButton,0,1);
setLayout(mainLayout);
setWindowTitle("ComboBox Crash");
comboBox->addItem("Item1");
comboBox->addItem("Item2");
comboBox->addItem("Item3");
connect(crashButton, SIGNAL(clicked()), this, SLOT(function()));
}
void ComboBoxCrash::function(){
comboBox->addItem("Item4");
}
#include <QtGui>
#include "GUI.h"
ComboBoxCrash::ComboBoxCrash(QWidget *parent) : QWidget(parent){
QComboBox *comboBox = new QComboBox;
QPushButton *crashButton = new QPushButton("Crash");
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(comboBox,0,0);
mainLayout->addWidget(crashButton,0,1);
setLayout(mainLayout);
setWindowTitle("ComboBox Crash");
comboBox->addItem("Item1");
comboBox->addItem("Item2");
comboBox->addItem("Item3");
connect(crashButton, SIGNAL(clicked()), this, SLOT(function()));
}
void ComboBoxCrash::function(){
comboBox->addItem("Item4");
}
To copy to clipboard, switch view to plain text mode
When the function which calls addItem is called, the application crashes.
I'm using Qt 4.5.2 and Qt Creator 1.2.0 under Windows XP SP2.
If anyone know why this happens, or even better, have a solution, I would be very happy.
Thanks in advance
Bookmarks