3
votes

I was making a test program where the MainWindow serves as a login screen. User types in a username and password. If they match what the string is assigned to, then the Dialog appears. If it fails, a QMessageBox appears instead.

What my issue is when I want the Dialog to appear (the main program), I want the Login page to disappear. The Command close(); would just close everything.

Here is the code for the MainWindow.cpp (The Dialog is referenced in the header as a POINTER called mDialog)

 void MainWindow::on_pushButton_clicked()
 {
 if (ui->lineEdit->text() == username)
 {
    if (ui->lineEdit_2->text() == password)
    {
        //This is where the Dialog appears
        mDialog= new MyDialog(this);
        mDialog->show();
    }
}
else if (ui->lineEdit->text() != username || ui->lineEdit->text() ==NULL)
{
    if (ui->lineEdit_2->text() != password || ui->lineEdit_2->text() == NULL)
    {
        QMessageBox::warning(this, "Error", "The username or password is incorrect.");
    }
 }
}

Here is the code for the main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}
5

5 Answers

5
votes

In Qt if the parent is destroyed, the children also, so if you go to this as a parameter to MyDialog, it will be destroyed. So that it is not destroyed do not pass the parent.

Change mDialog= new MyDialog(this) to mDialog= new MyDialog() and place close() after show().

The function would look like:

...
mDialog= new QDialog();
mDialog->show();
close();
...
3
votes

I think you should be showing the dialog as the login and the main window as the main program. If login is a success, show main window, not the other way around. Closing the main window is going to close the program.

I have done what you are trying to do. You can do what I said above or another option is to create a login screen in the main window using a QLabel. You can add an image to the Qlabel (a solid color image or anything you like) and make it the size of the window to block the view of the main program. Then you can add your line edits and buttons or anything you want. If login is successful, the image, labels and buttons can be closed to reveal the main program. I checked input using regular expressions.

1
votes

Use this->close() to close the current window, but in the constructor of MyDialog, don't pass anything to the constructor. By default, the constructor will pass 0 to the parent argument, so as a result, the dialog will not have a parent.

0
votes

If your main program is the Dialog you can before it have been shown open the login dialog with username/password fields.

Pseudocode for the main function (LoginDialog and MainDialog inherits QDialog):

QApplication a(argc, argv);
LoginDialog lDialog;
lDialog.exec(); // Modal dialog behavior. Stopped at this line while it not closed (QDialog::close())
if (lDialog.getUsername() != username || lDialog.getPassword() != password)
{
  return 0;
}
MainDialog mDialog;
mDialog.show();
return a.exec();
0
votes

You can set window visibility to false.

mainwindow.setVisible(false)