PDA

View Full Version : Proper use of an InputDialog box - capture CANCEL click



emp1953
9th November 2017, 21:13
I have the following function that is a SIMPLE password input dialog.

It works except that I cannot capture and process the CANCEL button click.
When CANCEL is clicked I want the dialog box to close without doing anything.
I have the cancel variable for the button click return but cannot get it done.

Here's the function


int class::display_password_dialog()
{
QString retarg;
bool cancel;

do
{
retarg = QInputDialot::gettext(0,"Password Entry", "Type Password, Press Ok: ",QLineEdit::Normal, "***", &cancel);
std::cout <<"user entered"<< retarg.toStdString()<<std::endl;
if(cancel)
{
std::cout<<"Cancel pressed"<<std::endl;
return EXIT_FAILURE;
}
} while (retarg != "'");
return EXIT_SUCCESS;
}

d_stranz
9th November 2017, 23:14
Read the docs again:


[static] QString QInputDialog::getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString &text = QString(), bool *ok = Q_NULLPTR, Qt::WindowFlags flags = Qt::WindowFlags(), Qt::InputMethodHints inputMethodHints = Qt::ImhNone)

Static convenience function to get a string from the user.

title is the text which is displayed in the title bar of the dialog. label is the text which is shown to the user (it should say what should be entered). text is the default text which is placed in the line edit. mode is the echo mode the line edit will use. inputMethodHints is the input method hints that will be used in the edit widget if an input method is active.

If ok is nonnull *ok will be set to true if the user pressed OK and to false if the user pressed Cancel. The dialog's parent is parent. The dialog will be modal and uses the specified widget flags.


Your logic in line 10 is exactly backward.

emp1953
10th November 2017, 17:42
I read that part of the docs several times and still implemented the logic backwards. Thank you for the quick solution.