I have a QLineEdit and a QPushButton.
The push button should be initially disabled, and should get enabled if and only if at least a character is in the input line.
Clicking the push button should result in a slot execution, to do something with the text the user has input in the line edit.
When the focus is in the input line and the user enters the enter key, the slot for click on push button should be executed.
It's quite easy to do that visually.
I change the property "enabled" of the push button, setting it to false (check box unchecked).
I right click on the line edit, and select the menu item "go to slot". I want to react to the change on the text, so I select the signal textChanged(QString). I'm sent to the code of the slot, automatically created by the IDE, and I add a line of code, like this:
void MainWindow::on_lineEdit_textChanged(QString text)
ui->pushButton->setEnabled(text.size() == 0 ? false : true);
That means the button is enabled only if the text is not empty. As required.
Now I want to specify what happens when the user clicks on the push button. So I right click on the button, menu item "go to slot" and the signal is clicked(). And then I have to modify this generated function:
Now I want to create an association between a line edit signal, the returnPressed() one that is called when the return key is pressed by the user, and a push button slot, the click() one that react at the user push.
So I go to the tabbed widget on bottom of the creator window, "Signals & Slots Editor" tab, I click on the green plus and I generate the connection:
Sender is lineEdit, Signal is returnPressed(), Receiver is pushButton, Slot is click().
This relation is saved in the xml for the ui, and recompiling we'll have the compiler generating the connection for us.