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. Clear QLineEdit on Click

Clear QLineEdit on Click

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.8k 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.
  • S Offline
    S Offline
    Shoaib Muhammad
    wrote on 4 Nov 2019, 05:40 last edited by
    #1

    Good day all,

    Hope everyone is well.

    I am a beginner programmer for QT 5.6 on an embedded platform.

    ISSUE #1:
    I wanted to do something specific with a QLineEdit object. On selecting the object (kind of like a mouse click), I want the current values to be cleared to be ready for the new values to be typed.

    For example, here is my QLineEdit named StrtEdit on my main window:

    Enter Value:|         |
    

    Upon entering a value:

    Enter Value:|123456789|
    

    And when I click on another QLineEdit the value would still show, but when I click on it again to enter data it should clear previous data and show:

    Enter Value:|         |
    

    Having a clear QPushButton makes sense but it's not currently an option due to the space limit of the application.

    Basically what I'm looking for is a 'clicked' signal from a QLineEdit.

    ISSUE #2:

    I could only delete the values of the QLineEdit using the 'backspace' key of a virtual keyboard on the first time I write data to a QLineEdit field. Once I click on something else and return to that input field, I am unable to delete the values using backspace and must use QlineEdit->setText(" "); instead. Why is that?

    Thank you for taking the time to read all this. Additional info available upon request.

    Kind regards,
    Shoaib

    J 1 Reply Last reply 4 Nov 2019, 05:43
    0
    • S Shoaib Muhammad
      4 Nov 2019, 05:40

      Good day all,

      Hope everyone is well.

      I am a beginner programmer for QT 5.6 on an embedded platform.

      ISSUE #1:
      I wanted to do something specific with a QLineEdit object. On selecting the object (kind of like a mouse click), I want the current values to be cleared to be ready for the new values to be typed.

      For example, here is my QLineEdit named StrtEdit on my main window:

      Enter Value:|         |
      

      Upon entering a value:

      Enter Value:|123456789|
      

      And when I click on another QLineEdit the value would still show, but when I click on it again to enter data it should clear previous data and show:

      Enter Value:|         |
      

      Having a clear QPushButton makes sense but it's not currently an option due to the space limit of the application.

      Basically what I'm looking for is a 'clicked' signal from a QLineEdit.

      ISSUE #2:

      I could only delete the values of the QLineEdit using the 'backspace' key of a virtual keyboard on the first time I write data to a QLineEdit field. Once I click on something else and return to that input field, I am unable to delete the values using backspace and must use QlineEdit->setText(" "); instead. Why is that?

      Thank you for taking the time to read all this. Additional info available upon request.

      Kind regards,
      Shoaib

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 4 Nov 2019, 05:43 last edited by
      #2

      @Shoaib-Muhammad said in Clear QLineEdit on Click:

      Why is that?

      I don't know. This should work. Are you doing anything else with this widgets?

      Regarding your first issue: you can subclass QLineEdit and overwrite https://doc.qt.io/qt-5/qlineedit.html#mousePressEvent

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 4 Nov 2019, 23:05
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 4 Nov 2019, 12:03 last edited by
        #3

        Hi,

        Just one thing about that design, you might get trouble with your users. What if they would like to copy the value ? Add a number ? Or more silly what if they accidentally click on it ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        4
        • J jsulm
          4 Nov 2019, 05:43

          @Shoaib-Muhammad said in Clear QLineEdit on Click:

          Why is that?

          I don't know. This should work. Are you doing anything else with this widgets?

          Regarding your first issue: you can subclass QLineEdit and overwrite https://doc.qt.io/qt-5/qlineedit.html#mousePressEvent

          S Offline
          S Offline
          Shoaib Muhammad
          wrote on 4 Nov 2019, 23:05 last edited by
          #4

          @jsulm @SGaist

          Hi guys,

          Fair point from SGaist. I might not do that anymore then.

          On the second issue, let me share some snippets of my code.

          header.h

          #pragma once
          
          #include <QWidget>
          #include <QPainter>
          #include <QApplication>
          #include <QPushButton>
          #include <QLabel>
          #include <QIntValidator>
          #include <Qt>
          #include <QtDebug>
          #include <QLineEdit>
          #include <QString>
          #include <QFont>
          #include <QGraphicsScene>
          #include <QFocusEvent>
          
          
          
          class CGui : public QWidget {
              
            Q_OBJECT
          
            public:
              CGui(QWidget *parent = 0);
          
              QLineEdit *StrtEdit;
              QLineEdit *StopEdit;
          
            private slots:
          
              void clickProcess();
              void clickDatacap();
              void clearField1();
              void clearField2();
          
          
            private:
              QLabel *rltxt;
              QLabel *imtxt;
          
              QLabel *strttxt;
              QLabel *stoptxt;
          
            protected:
              void paintEvent(QPaintEvent *e);
              void drawRectangles();
              void drawGraphs();
          
          };
          

          header.cpp

          #include "mainheader.h"
          #include <stdio.h>
          
          int rlval = 0;
          int imval = 0;
          
          /** Empty Arrays and Array Variables**/
          int rlarry[100];
          int imarry[100];
          
          /** Graph Variables **/
          int min_x1 = 80;
          int max_x1 = 380;
          int min_y1 = 33;
          int max_y1 = 369;
          int xlength1 = 0;
          int ylength1 = 0;
          int yspace1 = 0;
          int xspace1 = 0;
          int ytemp1 = 0;
          int xtemp1 = 0;
          
          int min_x2 = 470;
          int max_x2 = 735;
          int min_y2 = 236;
          int max_y2 = 380;
          int xlength2 = 0;
          int ylength2 = 0;
          int yspace2 = 0;
          int yspace2b=0;
          int xspace2 = 0;
          int ytemp2 = 0;
          int xtemp2 = 0;
           
          QString tex1 = QString("R: %1").arg(rlval);
          QString tex2 = QString("I: %1i").arg(imval);
          
          
          QFont font1("Fantasy", 15, QFont::Black);
          QFont font2("Fantasy", 10, QFont::Black);
          QFont font3("Fantasy", 20, QFont::Black);
          QFont font4("Fantasy", 10, QFont::Black);
          
          CGui::CGui(QWidget *parent)
              : QWidget(parent) {
          
          /***          Create Widgets          ***/
            
            /** Remove Titlebar **/   
            setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
            
            /** Buttons, Labels, and Input **/
            QPushButton *DcapBtn = new QPushButton("DATA CAPTURE", this);
            DcapBtn->setFont(font1);
            DcapBtn->setStyleSheet("background-color:rgb(249,56,34); border:none; color:white;");
            DcapBtn->setGeometry(602, 150, 193, 50);
          
            QPushButton *PrcBtn = new QPushButton("PROCESS", this);
            PrcBtn->setFont(font1);
            PrcBtn->setStyleSheet("background-color:rgb(65,182,230); border:none; color:white;");
            PrcBtn->setGeometry(405, 150, 193, 50);
          
            StrtEdit = new QLineEdit("",this);
            StrtEdit->setFont(font2);
            StrtEdit->setAlignment(Qt::AlignLeft);
            StrtEdit->setGeometry(555, 105, 240, 40);
          
            StopEdit = new QLineEdit("",this);
            StopEdit->setAlignment(Qt::AlignLeft);
            StopEdit->setFont(font2);
            StopEdit->setGeometry(555, 60, 240, 40);
          
            rltxt = new QLabel(" ", this);
            rltxt->setFont(font3);
            rltxt->setText(tex1);
            rltxt->setStyleSheet("color:white;");
            rltxt->setGeometry(415, 5, 192, 50);
           
            imtxt = new QLabel(" ", this);
            imtxt->setFont(font3);
            imtxt->setText(tex2); 
            imtxt->setStyleSheet("color:white;");
            imtxt->setGeometry(611, 5, 192, 50);
          
            strttxt = new QLabel("START FREQUENCY:",this);
            strttxt->setFont(font2);
            strttxt->setGeometry(410, 60, 145, 40);
            
            stoptxt = new QLabel("STOP FREQUENCY:",this);
            stoptxt->setFont(font2);
            stoptxt->setGeometry(410, 105, 145, 40);
          
            /** Plots **/
          
          
            /**  Functions **/
            connect(DcapBtn, &QPushButton::clicked, this, &CGui::clickDatacap);
            connect(PrcBtn, &QPushButton::clicked, this, &CGui::clickProcess);
          }
          
          
          void CGui::clickProcess(){
            qDebug() << "Process Button Clicked";
          }
          
          void CGui::clickDatacap(){
            qDebug() << "Data Capture Button Clicked";
          }
          

          Once again, when I run this code on the target, I could only use backspace the first time I write something on QLineEdit. Once I click somewhere and return to it I could only backspace the new values that I place.

          Also, please feel free to correct my coding methods for Qt. I'm sure my code can be improved. Thank you everyone.

          Kind regards,
          Shoaib

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 4 Nov 2019, 23:30 last edited by
            #5

            Why not use a QFormLayout to build your GUI ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply 5 Nov 2019, 03:49
            0
            • S SGaist
              4 Nov 2019, 23:30

              Why not use a QFormLayout to build your GUI ?

              S Offline
              S Offline
              Shoaib Muhammad
              wrote on 5 Nov 2019, 03:49 last edited by
              #6

              @SGaist said in Clear QLineEdit on Click:

              Why not use a QFormLayout to build your GUI ?

              I felt like I had more control over the precise positioning of elements in the GUI if I used absolute position instead of QFormLayout.

              One issue I had was that the current Qt I was using didn't have QT+= charts in the sdk as well as the target board. I had no way to work around this and so I used QPainter to draw the entire line graph. That we when I decided to use absolute positioning, since the app will be used only on a specific display.

              Would it have any effect on the functionality of the design?

              Thank you,
              Shoaib

              aha_1980A 1 Reply Last reply 5 Nov 2019, 05:45
              0
              • S Shoaib Muhammad
                5 Nov 2019, 03:49

                @SGaist said in Clear QLineEdit on Click:

                Why not use a QFormLayout to build your GUI ?

                I felt like I had more control over the precise positioning of elements in the GUI if I used absolute position instead of QFormLayout.

                One issue I had was that the current Qt I was using didn't have QT+= charts in the sdk as well as the target board. I had no way to work around this and so I used QPainter to draw the entire line graph. That we when I decided to use absolute positioning, since the app will be used only on a specific display.

                Would it have any effect on the functionality of the design?

                Thank you,
                Shoaib

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 5 Nov 2019, 05:45 last edited by
                #7

                Hi @Shoaib-Muhammad,

                one thing to note is, that QLineEdit already has a clearButton. But I'm not sure how much that will help you, as the default implementation clears everything. But probably you can detect that and print your prefix again.

                Regards

                Qt has to stay free or it will die.

                S 1 Reply Last reply 5 Nov 2019, 23:25
                1
                • aha_1980A aha_1980
                  5 Nov 2019, 05:45

                  Hi @Shoaib-Muhammad,

                  one thing to note is, that QLineEdit already has a clearButton. But I'm not sure how much that will help you, as the default implementation clears everything. But probably you can detect that and print your prefix again.

                  Regards

                  S Offline
                  S Offline
                  Shoaib Muhammad
                  wrote on 5 Nov 2019, 23:25 last edited by
                  #8

                  @aha_1980 said in Clear QLineEdit on Click:

                  Hi @Shoaib-Muhammad,

                  one thing to note is, that QLineEdit already has a clearButton. But I'm not sure how much that will help you, as the default implementation clears everything. But probably you can detect that and print your prefix again.

                  Regards

                  Hi aha_1980,

                  Thank you for this. This works as an alternative. I have read this function somewhere and completely forgot about it.

                  Thank you everyone for your inputs. I'll mark this thread solved.

                  Kind regards,
                  Shoaib

                  1 Reply Last reply
                  1

                  1/8

                  4 Nov 2019, 05:40

                  • Login

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