mlovick
mlovick

Reputation: 9

Qt Program has unexpectedly finished for no reason

I am new to Qt and trying to make my first Qt application. I found that I am getting "the program has unexpectedly finished" error for no apparent reason to me. I commented out all the code and uncommented them line by line and it seems to be only certain lines of my code that are generating this error. I can't for the life of me understand why this would be happening.

I have a main window that works perfectly fine. All the layouts and connections work, etc. When I click a certain button, it opens a new window. This window also works perfectly fine. When a certain button is clicked, a third window is opened. This is where the issue lies. For no apparent reason, specific lines are generating problems. I have a vertical layout object and when I try to addLayout(layout_horizontal) I get the unexpected finish error. In the next line after that, I add a grid layout with no issues, and can add two buttons to the horizontal layout with no issues. Why would my addLayout(layout_horizontal) not work but addLayout(layout_grid) does?

I do a button[i][j] = new QPushButton(this) and this also generates the same error. It is not an issue with my pointers because the same code runs fine in my Xcode environment.

What baffles me the most is that my this->size = size line causes this unexpected finish error! I have a private int size declared in my header, and then have int size parameter in my constructor. Why would that assignment cause my program to unexpectedly finish? That seems to make no sense to me at all.

And even more than that, if I copy my entire .h and .cpp files and paste them into a different Qt project in a different folder, the code works perfectly fine. No unexpected finish. Nothing.

Could it be that my project is somehow corrupted? Or there is a bug in the Qt development environment that is causing this code to be compiled incorrectly and cause a runtime error?

I am so confused and frustrated because the my code should work but it doesn't.

I have attached the code below.

This code works fine

GameWindow::GameWindow(int size, QWidget *previous) : QWidget() {

    this->previous = previous;

    int button_size = 0;

    switch (size) {

    case 10:
        button_size = 50;
        break;
    case 20:
        button_size = 35;
        break;
    case 30:
        button_size = 30;
        break;

    }

    //this->size = size; NOTICE THIS LINE IS COMMENTED
    map = new Button**[size];
    button_flag = new QPushButton(tr("&Flag"), this);
    button_quit = new QPushButton(tr("&Quit"), this);
    layout_grid = new QGridLayout;
    layout_vertical = new QVBoxLayout;
    layout_horizontal = new QHBoxLayout;

    //set up the layout
    this->setLayout(layout_vertical);
    //layout_vertical->addLayout(layout_horizontal);
    layout_vertical->addLayout(layout_grid);
    layout_horizontal->addWidget(button_flag);
    layout_horizontal->addWidget(button_quit);
    layout_grid->setSpacing(0);

    //set up buttons
    button_flag->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    button_quit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    button_flag->setFixedHeight(50);
    button_quit->setFixedHeight(50);
    connect(button_quit, SIGNAL(clicked()), this, SLOT(close()));
    connect(button_flag, SIGNAL(clicked()), this, SLOT(close()));

    for (int i = 0; i < size; i++) {
        map[i] = new Button*[size];
        for (int j = 0; j < size; j++) {
            //map[i][j] = new Button(this);
            //map[i][j]->setFixedSize(button_size, button_size);
            //layout_grid->addWidget(map[i][j], i, j);
            //connect(map[i][j], SIGNAL(clicked()), map[i][j], SLOT(button_clicked()));
        }
    }

    this->setFixedSize(this->minimumWidth(), this->minimumHeight());
    initialize_bombs();

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            count_neighboring_bombs(i, j);
        }
    }

}

This code unexpectedly finishes

GameWindow::GameWindow(int size, QWidget *previous) : QWidget() {

    this->previous = previous;

    int button_size = 0;

    switch (size) {

    case 10:
        button_size = 50;
        break;
    case 20:
        button_size = 35;
        break;
    case 30:
        button_size = 30;
        break;

    }

    this->size = size; //NOTICE THIS LINE IS NO LONGER COMMENTED
    map = new Button**[size];
    button_flag = new QPushButton(tr("&Flag"), this);
    button_quit = new QPushButton(tr("&Quit"), this);
    layout_grid = new QGridLayout;
    layout_vertical = new QVBoxLayout;
    layout_horizontal = new QHBoxLayout;

    //set up the layout
    this->setLayout(layout_vertical);
    //layout_vertical->addLayout(layout_horizontal);
    layout_vertical->addLayout(layout_grid);
    layout_horizontal->addWidget(button_flag);
    layout_horizontal->addWidget(button_quit);
    layout_grid->setSpacing(0);

    //set up buttons
    button_flag->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    button_quit->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    button_flag->setFixedHeight(50);
    button_quit->setFixedHeight(50);
    connect(button_quit, SIGNAL(clicked()), this, SLOT(close()));
    connect(button_flag, SIGNAL(clicked()), this, SLOT(close()));

    for (int i = 0; i < size; i++) {
        map[i] = new Button*[size];
        for (int j = 0; j < size; j++) {
            //map[i][j] = new Button(this);
            //map[i][j]->setFixedSize(button_size, button_size);
            //layout_grid->addWidget(map[i][j], i, j);
            //connect(map[i][j], SIGNAL(clicked()), map[i][j], SLOT(button_clicked()));
        }
    }

    this->setFixedSize(this->minimumWidth(), this->minimumHeight());
    initialize_bombs();

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            count_neighboring_bombs(i, j);
        }
    }

}

I know that sometimes errors will arise but cleaning the project and running qmake before building again fixes it. Maybe the solution is some sort of trick like that. Thanks for any help.

Upvotes: 0

Views: 360

Answers (1)

CharonX
CharonX

Reputation: 2207

I assume that you have no (own) size member in the GameWindow class.

If so, then you are assigning an int to QWidget's size member, which is of type QSize. I would expect the compiler to complain, but I as I don't know how your header looks like it is difficult to tell.

If you have a int size member in GameWindow that may also be a cause, as QWidget has already have a member with that name...

Upvotes: 1

Related Questions