Protecting against invalid input

The configuration dialog did not have any validation until now. Let's make it such that the button to accept the dialog is only enabled when neither of the two line edits is empty (that is, when both the fields contain player names). To do this, we need to connect the textChanged signal of each line edit to a slot that will perform the task.

First, go to the configurationdialog.h file and create a private slot void updateOKButtonState(); in the ConfigurationDialog class (you will need to add the private slots section manually). Use the following code to implement this slot:

void ConfigurationDialog::updateOKButtonState() { QPushButton *okButton = ui->buttonBox->button(QDialogButtonBox::Ok); okButton->setEnabled(!ui->player1Name->text().isEmpty() ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.