c++ - QMainWindow 在 show() 后立即关闭

标签 c++ qt qt5

我是 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/

相关文章:

c++ - Qt5应用部署qwindows.dll搜索路径顺序

c++ - Qt 5中String的Base64编码

c++ - 一堆函数指针 : how to call the function?

c++ - 为什么在 std::cout 中使用 "::"运算符而不是 "."运算符?

c++ - 我应该使用对象作为图 block 吗?

c++ - QPainter::begin:绘制设备返回的引擎== 0,键入:2

C++ - 从类内部包含 typedef 结构

c++ - 通过Qt读取xml

c++ - QSettings 在没有管理员权限的情况下不保存 ini 更改

c++ - 如何在 QT 中创建具有多个属性的按钮?