QLineEdit Selection changed()
-
hello I am implementing a keypad with QFrame and push buttons buttons. the input widget is LineEdit following is my code.
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->b0_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b1_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b2_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b3_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b4_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b5_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b6_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b7_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b8_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b9_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->bminus_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->b_point_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->clear_5,SIGNAL( clicked() ), this, SLOT( handler() )); connect(ui->back_5,SIGNAL( clicked() ), this, SLOT( handler() )); op=""; } MainWindow::~MainWindow() { delete ui; } /*----------------------------------------------------------------------------------------------------------------- keypad and related functions ------------------------------------------------------------------------------------------------------------------*/ void MainWindow::handler() { QPushButton *buttons = (QPushButton *)sender(); //mapping signals ip = buttons->text(); if (ip=="Clr") { switch(LineEdit_select) { case 0: ui->ln_1->clear(); break; case 1: ui->ln_2->clear(); break; case 2: ui->ln_3->clear(); break; case 3: ui->ln_4->clear(); break; default:break; } } else if(ip=="Back") { switch(LineEdit_select) { case 0: op=ui->ln_1->text(); if(op!="") op.remove(op.length()-1,op.length(); ui->ln_1->setText(op); break; case 1: op=ui->ln_2->text(); if(op!="") op.remove(op.length()-1,op.length(); ui->ln_2->setText(op); break; case 2: op=ui->ln_3->text(); if(op!="") op.remove(op.length()-1,op.length(); ui->ln_3->setText(op); break; case 3: op=ui->ln_4->text(); if(op!="") op.remove(op.length()-1,op.length(); ui->ln_4->setText(op); break; default:break; } } else { op = ip; switch(LineEdit_select) { case 0: ui->ln_1->setText(ui->ln_1->text()+op); break; case 1: ui->ln_2->setText(ui->ln_2->text()+op); break; case 2: ui->ln_3->setText(ui->ln_3->text()+op); break; case 3: ui->ln_4->setText(ui->ln_4->text()+op); break; default:break; } } } void MainWindow::on_ln_1_selectionChanged() { op=""; LineEdit_select=0; } void MainWindow::on_ln_2_selectionChanged() { op=""; LineEdit_select=1; } void MainWindow::on_ln_3_selectionChanged() { op=""; LineEdit_select=2; } void MainWindow::on_ln_4_selectionChanged() { op=""; LineEdit_select=3; } void MainWindow::on_BCLx_clicked() { this->close(); }
Code works Fine but i always have to double click on the lineEdits. ln_1 to ln_4 are the four lineEdits. I want this to work on single click. Any suggestions?
-
Code works Fine but i always have to double click on the lineEdits. ln_1 to ln_4 are the four lineEdits. I want this to work on single click. Any suggestions?
Hi,
It's not clear what the single click or double click refers to. Is the intention to setFocus() on a QLineEdit?
As far as suggestions, it's hard without a complete, minimal working example.
For my taste, having many clicked signals connect to a single slot makes for a complicated slot implementation. Rather than an if/else chain to select the operation, and then a switch to select the input/output, I would use a slot per operation. The operations covered by the else clause can use a lambda to pass
differentiating arguments to a single slot.Rather than LineEdit_select being an integer, it looks like a pointer to the current ui->ln_* could work instead.
eg:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { <type of ui->ln_*> *lineEdit_select = &ui->ln_1; connect(ui->b0_5, &Type::clicked, this, [&](){ this->on_number(QChar("0")); }); connect(ui->b1_5, &Type::clicked, this, [&](){ this->on_number(QChar("1")); }); connect(ui->clear_5, &Type::clicked, this, &MainWindow::on_clear); } void MainWindow::on_clear() { lineEdit_select->clear(); } void MainWindow::on_number(QChar number) { lineEdit_text->setText(lineEdit_text->text() + number); } void MainWindow::on_ln_1_selectionChanged() { lineEdit_select = &ui->ln_1; }