隐藏窗口时退出Qt
Qt exit when hiding window
我有一个MainWindow
,它在构造函数中调用LoginWindow。LoginDialog
有一个按钮,用于创建一个帐户,该帐户将创建一个QDialog
。
我想在新帐户的对话框显示时隐藏LoginDialog
,但不知何故它崩溃了。
当我删除隐藏并显示LoginDialog
的功能的第一行和最后一行时,它绝对没问题。为什么它崩溃与hide()
和show()
被称为?
void LoginDialog::createAccount()
{
// (-> will cause crash later) hide(); //Hides LoginDialog
QDialog dlg;
dlg.setGeometry( this->x(), this->y(), this->width(), this->height() );
QWidget* centralWidget = new QWidget( &dlg );
QVBoxLayout* l = new QVBoxLayout( centralWidget );
dlg.setLayout( l );
QLineEdit *dlgUser = new QLineEdit( centralWidget );
QLineEdit *dlgPass = new QLineEdit( centralWidget );
dlgPass->setEchoMode( QLineEdit::Password );
l->addWidget( new QLabel( tr("Username :"), centralWidget ) );
l->addWidget( dlgUser );
l->addWidget( new QLabel( tr("Password :"), centralWidget ) );
l->addWidget( dlgPass );
l->addWidget( new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, centralWidget ) );
if( dlg.exec() != QDialog::Rejected )
{
;
}
delete centralWidget;
// (-> will cause crash later) show(); //Show LoginDialog again
}
没有错误,它只是意外崩溃,有时它会以代码 (0) 退出。
当使用调试器进行分析并真正完成每个步骤时,它不会崩溃。将显示LoginDialog
,并且不会崩溃。
我在对话框中不明白您的centralWidget
目的?我认为根本不需要它,您可以直接在对话框中组装您的小部件。我会以这种方式重写您的代码:
void LoginDialog::createAccount()
{
QDialog dlg;
dlg.setGeometry( this->x(), this->y(), this->width(), this->height() );
QLineEdit *dlgUser = new QLineEdit( &dlg );
QLineEdit *dlgPass = new QLineEdit( &dlg );
dlgPass->setEchoMode( QLineEdit::Password );
QVBoxLayout* l = new QVBoxLayout;
l->addWidget( new QLabel( tr("Username :"), &dlg ) );
l->addWidget( dlgUser );
l->addWidget( new QLabel( tr("Password :"), &dlg ) );
l->addWidget( dlgPass );
l->addWidget( new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dlg ) );
dlg.setLayout( l );
if( dlg.exec() != QDialog::Rejected )
{
// Do something.
}
}
相关文章:
- 运行Qt时处理退出代码0xC0000135 hello world
- QT和新的信号槽语法,QApplication::退出主功能外的插槽
- 对 QMainWindow 使用 std::unique_ptr 时退出时出现 QT 应用程序分段错误
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- 当MainWindow隐藏时,QT应用程序会自动退出
- 当我要退出应用程序时,如何在QT中自动调用destructor
- Qt,C++,如何退出QThread
- 如何使用工作线程正确退出Qt应用程序
- QT MainWindow已退出时,用QTCreator编写的DLIB代码未退出
- Qt - 意外的 GDB 退出
- QT QTcpServer in thread;如何在退出时关闭侦听服务器
- Qt:对于生产者-消费者模式中的消费者来说,这是正确的退出方式吗
- 在退出应用程序时安全退出Qt线程
- QT/C++,OS X:退出应用程序时可选择隐藏(),以便应用程序继续运行,并在单击dock中的图标后再次显示
- Qt退出事件循环
- 将opencv库添加到QT创建者并获取错误:链接器命令失败,退出代码为1
- 多线程Qt应用程序在退出时不会停止
- 使用Windows中另一个进程的事件循环向Qt应用程序发送退出消息
- 使Qt应用程序在最后一个窗口关闭时不退出
- QT退出不调用主窗口析构函数