QMainWindow closes right after show()

僤鯓⒐⒋嵵緔 提交于 2019-12-20 05:39:11

问题


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
}

回答1:


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.



来源:https://stackoverflow.com/questions/36944749/qmainwindow-closes-right-after-show

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!