Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit Selection changed()
Forum Update on Monday, May 27th 2025

QLineEdit Selection changed()

Scheduled Pinned Locked Moved Solved General and Desktop
qt for windowsqlineeditqmouseevent
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrex_Qt
    wrote on last edited by
    #1

    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?

    jeremy_kJ 1 Reply Last reply
    0
    • A Andrex_Qt

      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?

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      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;
      }
      

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Andrex_Qt
        wrote on last edited by
        #3

        Thanks for the insight, still my issue was that you have to double click (mouse or touch) the LineEdit to get its selection. Now in place of LineEdit i have used TextEdit, it works on single click. Thanks again for the reply.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved