-1

Following is my definition of confirm window( the window that is opened on clicking submit button in main window). confirmwindow.h

#ifndef CONFIRMWINDOW_H
#define CONFIRMWINDOW_H

#include <QDialog>

namespace Ui {
class ConfirmWindow;
}

class ConfirmWindow : public QDialog
{
    Q_OBJECT

public:
    explicit ConfirmWindow(QWidget *parent = 0);
    ConfirmWindow(QString name);
    ~ConfirmWindow();

private:
    Ui::ConfirmWindow *ui;
};

#endif // CONFIRMWINDOW_H

And in confirmwindow.cpp I defined in following manner

#include "confirmwindow.h"
#include "ui_confirmwindow.h"

ConfirmWindow::ConfirmWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConfirmWindow)
{
    ui->setupUi(this);
}

ConfirmWindow::~ConfirmWindow()
{
    delete ui;
}

ConfirmWindow::ConfirmWindow(QString name)
{
    ui->label_show_name->setText(name);
}

when I run the program it all goes well untill I press submit button, which crashes the app with a message in console as The program has unexpectedly finished.

In mainwindow.cpp I've called it as

void MainWindow::on_pushButton_submit_clicked()
{
    ConfirmWindow confirm_window(ui->lineEdit_name->text());
    confirm_window.exec();
}

I guess, I am defining the constructor in a wrong way in confirmwindow.cpp file.

  • 1
    Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – litelite Oct 02 '17 at 19:51
  • 2
    You have two constructors, one of which does not call the constructor of QDialog. and does not initialize ui. You better check check constructor you are using when creating the Dialog... – Xatyrian Oct 02 '17 at 19:52

1 Answers1

0

This line:

ConfirmWindow confirm_window(ui->lineEdit_name->text());

will create a ConfirmWindow using the constructor ConfirmWindow(String). However in this constructor you do not initialize ConfirmWindow::ui, the consequence is thatui->label_show_name has undefined behaviour.

Quick Fix:

ConfirmWindow::ConfirmWindow(QString name) :
   ui(new Ui::ConfirmWindow) // initialize UI
{
    ui->label_show_name->setText(name);
}

Here are some more fixes:

#ifndef CONFIRMWINDOW_H
#define CONFIRMWINDOW_H

#include <QDialog>

namespace Ui {
class ConfirmWindow;
}

class ConfirmWindow : public QDialog
{
    Q_OBJECT

public:
    explicit ConfirmWindow(QWidget *parent = 0);
    explicit ConfirmWindow(QString name, QWidget *parent = 0); // Prevent implicit conversion from QString
    ~ConfirmWindow();

private:
    Ui::ConfirmWindow *ui = nullptr; // Require C++11, but will make easier to debug cases where you forgot to initialize ui
};

#endif // CONFIRMWINDOW_H

-

#include "confirmwindow.h"
#include "ui_confirmwindow.h"

ConfirmWindow::ConfirmWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConfirmWindow)
{
    ui->setupUi(this);
}

ConfirmWindow::~ConfirmWindow()
{
    delete ui;
}

ConfirmWindow::ConfirmWindow(QString name, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConfirmWindow)
{
    ui->label_show_name->setText(name);
}
Benjamin T
  • 8,120
  • 20
  • 37