0

I am using Qt Creator to make a UI.
UI consists of two or more QLineEdits and ten QPushButtons to input 0-9 numberic characters to QLineEdits. How can I enter 0-9 number strings in both QLineEdits one by one.

If I press QPushButton with label '5'and cursor is on QLineEdit (say QLineEdit 1) it should append '5' in QLineEdit 1 or if QLineEdit 2 is selected it should append '5' in QLineEdit 2 and respectively with the other QPushButtons also.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Saad Rafey
  • 531
  • 2
  • 6
  • 18

2 Answers2

0

you could have a slot in your ui class like the following

void MyDialog::numberButtonPressed()
{
    QPushButton* btn = qobject_cast<QPushButton*>(QObject::sender());
    if (!btn)
        return; // TODO error handling
    ui.lineEdit->setText(ui.lineEdit->text() + btn->text());
}

and then QObject::connect all numeric buttons to that slot.

cheers

Zaiborg
  • 2,492
  • 19
  • 28
  • 1
    To complete your answer: check which QLineEdit has focus to change its text: `QLineEdit* lineEdit = ui.lineEdit->hasFocus() ? ui.lineEdit : ui.lineEdit2 ; lineEdit->setText(lineEdit->text() + btn->text())` – Frodon Nov 24 '15 at 16:53
  • Thanks for your replies. I am unable to interpret your answers properly can you please elaborate. Please make sure I am working on QT Creator – Saad Rafey Nov 25 '15 at 02:31
0

In QT Creator after adding pushbutton to slots in UI, go to function and check if it has focus or not with hasFocus().

e.g.

void MainWindow::on_pushButton_clicked()
{
    if(ui->lineEdit_1->hasFocus)
    {
        ui->lineEdit_1->setText("your text");
    }
    else if(ui->lineEdit_2->hasFocus)
    {
        ui->lineEdit_2->setText("your text");
    }
}
Saad Rafey
  • 531
  • 2
  • 6
  • 18