Clear QLineEdit on Click
-
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 -
@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
-
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 ?
-
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 -
Why not use a QFormLayout to build your GUI ?
-
@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 -
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
-
@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