Your _ui
variable is not initialized, that's why it crashes. You need something like: _ui = new Ui::MainWindow();
... also, it's good habit to have MainWindow
take a QObject
parameter as the parent in the second constructor. Or just use the first one and add the extra parameters you need.
QMainWindow crashes when using QComboBox [closed]
-
01-10-2022 - |
Question
I have a problem with my application in Qt, I use a QMainWindow and try to set up 2 QComboBox like I did for other widgets but the application crashes at the setupUi :
Voilà ma classe MainWindow, elle fonctionnait parfaitement et s'affichait jusqu'à ce que j'ajoute les 2 Combobox.
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
MainWindow(Map *inMap, int dim1, int dim2);
~MainWindow();
public slots:
void boutonClicked();
private:
Ui::MainWindow *_ui;
QPushButton *_bouton;
Canvas *_canvas;
QComboBox *_boxDim1;
QComboBox *_boxDim2;
};
Une fois les ComboBox ajoutées, le programme crashe à l'exécution du constructeur :
MainWindow::MainWindow(Map *inMap, int dim1, int dim2)
{
_ui->setupUi(this);
_boxDim1 = new QComboBox();
_boxDim2 = new QComboBox();
_canvas = new Canvas(inMap, dim1, dim2);
_bouton = new QPushButton("Test !");
_bouton->setToolTip("Bouton a push");
_bouton->setCursor(Qt::PointingHandCursor);
connect(_bouton, SIGNAL(clicked()), this, SLOT(boutonClicked()));
QWidget *q = new QWidget();
setCentralWidget(q);
QGridLayout *mainLayout = new QGridLayout();
mainLayout->addWidget(_canvas, 1, 0, 1, 1);
mainLayout->addWidget(_bouton, 2, 0, 1, 1);
q->setLayout(mainLayout);
}
Après débuggage dans ddd, le point exact du segfault correspond à :
_ui->setupUi(this);
Et à l'intérieur de cette fonction :
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objetName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
centralwidget = new QWidget(MainWindow) /// Segfault ici.
Je dois avouer que je ne saisis pas d'où vient le problème puisque l'ajout et la création du QPushButton et du Canvas ont quant à eux parfaitement fonctionné.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow