Eduard
Eduard

Reputation: 536

QMainWindow closes right after show()

I am new to Qt (use Objective-C mostly) so I am stuck with probably noob issue. From the QDialog window I try to open QMainWindow like this:

this->close();
SQLWindow window;
window.receivePath(path); //Path for the .sqlite file
window.show()

QDialog closes and for millisecond I see a glimpse of a new window, but it closes too. Below is QMainWindow part:

SQLWindow::SQLWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::SQLWindow) 
{ 
    ui->setupUi(this); 
    this->initialSetup(); 
} 

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

void SQLWindow::initialSetup() 
{ 
    ui->tableView->setSortingEnabled(true); 
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); 
} 

void SQLWindow::receivePath(QString path) 
{ 
   this->openDatabase(path); 
} 

void SQLWindow::openDatabase(QString path) 
{
    //Opening database just fine
}

Upvotes: 1

Views: 1433

Answers (1)

Oberon
Oberon

Reputation: 3249

Your window is a local variable it is destroyed at the end of the function and hence the destructor closes it. What you could do is create the SQLWindow on the heap with new SQLWindow and e.g. use the Qt::WA_DeleteOnClose attribute as shown here.

Alternatively, a better design might be to create both the dialog and the window as local variables of the main function and let the main function pass the path from the dialog to the SQLWindow, then you need no new.

Upvotes: 3

Related Questions