0

I have a MainWindow which calls the LoginWindow in the constructor. The LoginDialog has a button to create an account which will create a QDialog.

I wanted to hide the LoginDialog while the Dialog for the new account is showing but somehow it crashes.

When I remove the first and last line of the function which hides and shows the LoginDialog it's absolutely fine. Why does it crash with hide() and show() are called ?

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
}

There are no errors, it just crashes unexpectedly and sometimes it exits with code (0).

When analyzing with the debugger and really going through every single step it doesn't crash. The LoginDialog will be shown and it's not going to crash.

Davlog
  • 2,162
  • 8
  • 36
  • 60

1 Answers1

0

I don't get the purpose of your centralWidget in the dialog? I don't think it is needed at all, and you can assemble your widgets directly in the dialog. I would rewrite your code in this way:

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.
    }
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • @Davlog, ah, you hide the parent dialog while you open another modal child dialog that creates an account? Well, what if you just call `dlg.show()` instead of calling `dlg.exec()`? – vahancho Oct 23 '13 at 15:16
  • hmm... I replaced the if statement with dlg.show() but it still crashes. – Davlog Oct 23 '13 at 15:19
  • @Davlog, is the back trace for crash available? – vahancho Oct 23 '13 at 15:22
  • QLabel and QDialogButtonBox do not need parent, since they are placed in a layout, which will reparent them – BЈовић Oct 23 '13 at 15:28
  • @vahancho I tried to check with the debugger where it crashes but everytime I go through it with the debugger it works... no crash, no bug :/ – Davlog Oct 23 '13 at 15:31
  • @Davlog, what if you hide the LoginDialog **after** showing the secondary dialog? – vahancho Oct 24 '13 at 06:30