0
votes

I am using QT 4.8 and C++ and I need a default push button which will be pressed when some text are input in a lineEdit and ENTER key is pressed.

According to the QT document for the property "default" of pushbutton. the behavior is only for dialogs.

"The default button behavior is provided only in dialogs. Buttons can always be clicked from the keyboard by pressing Spacebar when the button has focus."

How can I have this behavior for a widget, which is a container for all my GUI components.

Thanks.

1
Can you post some sample code which you have tried out?Sumeet
@Sumeet Thanks. I just set the property "default" of this button true by QtDesigner.Chansy

1 Answers

0
votes

You can connect the signal QLineEdit::returnPressed() to the slot QAbstractButton::click().

Method 1

from the Designer you simply can:

  1. Right-click on lineEdit
  2. Select "Go to slot..."

enter image description here

  1. Choose returnPressed():

enter image description here

  1. Press ok
  2. In the code view you will see the slot connected to the chosen signal
  3. You can simply call the click() slot of a button you need:

enter image description here

Method 2

You can manually connect signal of any lineEdit to a slot of any pushButton:

In Qt 4 syntax:

connect(ui->lineEdit, SIGNAL(returnPressed()),
            ui->anyButton, SLOT(click()));

In new Qt 5 syntax:

connect(ui->lineEdit, &QLineEdit::returnPressed,
            ui->anyButton, &QPushButton::click);