我是 Qt 的新手(主要使用 Objective-C),所以我可能遇到了菜鸟问题。在 QDialog
窗口中,我尝试像这样打开 QMainWindow
:
this->close();
SQLWindow window;
window.receivePath(path); //Path for the .sqlite file
window.show()
QDialog
关闭,几毫秒后我看到了一个新窗口,但它也关闭了。下面是 QMainWindow
部分:
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
}
最佳答案
您的窗口是一个局部变量,它在函数结束时被销毁,因此析构函数将其关闭。
您可以做的是使用 new SQLWindow
在堆上创建 SQLWindow
,例如使用 Qt::WA_DeleteOnClose
属性,如图所示 here .
或者,更好的设计可能是将对话框和窗口都创建为 main
函数的局部变量,并让 main 函数将路径从对话框传递到 SQLWindow
,那么你不需要new
。
关于c++ - QMainWindow 在 show() 后立即关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944749/