QMainWindow在show()之后立即关闭 [英] QMainWindow closes right after show()

查看:111
本文介绍了QMainWindow在show()之后立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt的新手(主要使用Objective-C),所以我可能遇到了菜鸟问题.在 QDialog 窗口中,我尝试像这样打开 QMainWindow :

  this-> close();SQLWindow窗口;window.receivePath(path);//.sqlite文件的路径window.show() 

QDialog 关闭,在毫秒内,我看到了一个新窗口的瞥见,但它也关闭了.以下是 QMainWindow 部分:

  SQLWindow :: SQLWindow(QWidget * parent):QMainWindow(parent),ui(新Ui :: SQLWindow){ui-> setupUi(this);this-> initialSetup();}SQLWindow ::〜SQLWindow(){删除ui;}无效的SQLWindow :: initialSetup(){ui-> tableView-> setSortingEnabled(true);ui-> tableView-> setEditTriggers(QAbstractItemView :: NoEditTriggers);}无效的SQLWindow :: receivePath(QString路径){这-> openDatabase(路径);}无效的SQLWindow :: openDatabase(QString路径){//打开数据库就好了} 

解决方案

您的窗口是一个局部变量,在函数末尾被销毁,因此析构函数将其关闭.您可以做的是使用 new SQLWindow 在堆上创建 SQLWindow ,例如如此处所示,使用 Qt :: WA_DeleteOnClose 属性.

或者,更好的设计可能是将对话框和窗口都创建为 main 函数的局部变量,然后让main函数将对话框的路径传递给 SQLWindow ,那么您就不需要 new .

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
}

解决方案

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.

这篇关于QMainWindow在show()之后立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆