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 does not clear its `text()` after the clear button is clicked
Forum Updated to NodeBB v4.3 + New Features

QLineEdit does not clear its `text()` after the clear button is clicked

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.0k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote last edited by
    #1

    Using Qt 5.15.3 on Ubuntu 24.04.3:

    I have a line edit widget with the 'clear' button enabled. I can connect my code to its signal (QAction::triggered(), there is some code on this forum which shows how to do it) which forwards to another function which is a slot connected to the editingFinished() signal of the line edit.

    In the slot connected to editingFinished(), I query the line edit's text() method -- this should return an empty string when the clear button is clicked; however, it shows me the old content, although it is no longer visible in the line edit widget.

    The code works fine when the slot is called normally by tabbing out of the line edit widget.

    Is this a known bug?

    Christian EhrlicherC 1 Reply Last reply
    0
    • R Robert Hairgrove

      Using Qt 5.15.3 on Ubuntu 24.04.3:

      I have a line edit widget with the 'clear' button enabled. I can connect my code to its signal (QAction::triggered(), there is some code on this forum which shows how to do it) which forwards to another function which is a slot connected to the editingFinished() signal of the line edit.

      In the slot connected to editingFinished(), I query the line edit's text() method -- this should return an empty string when the clear button is clicked; however, it shows me the old content, although it is no longer visible in the line edit widget.

      The code works fine when the slot is called normally by tabbing out of the line edit widget.

      Is this a known bug?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote last edited by
      #2

      @Robert-Hairgrove said in QLineEdit does not clear its `text()` after the clear button is clicked:

      Is this a known bug?

      Yes, it's known as a programmers error.
      you are doing something wrong. Please provide a minimal, compileable example of the problem.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @Robert-Hairgrove said in QLineEdit does not clear its `text()` after the clear button is clicked:

        Is this a known bug?

        Yes, it's known as a programmers error.
        you are doing something wrong. Please provide a minimal, compileable example of the problem.

        R Offline
        R Offline
        Robert Hairgrove
        wrote last edited by Robert Hairgrove
        #3

        @Christian-Ehrlicher Very funny.

        Here is some example code -- a screenshot was taken and attached at the end after the code.
        Built with Qt 6.9.1 and gcc 13 on Ubuntu 24.04.3 LTS.

        NOTE: The slot which is connected to the clear button's QAction calls "handleEditingFinished()", which is where it would seem to be expected that calling "QLineEdit::text()" should return an empty string. However, it only returns the empty string in the "textChanged()" slot.

        Project file (qmake): "Test_LineEdit.pro"

        QT += core gui widgets
        CONFIG += c++17
        SOURCES += \
            main.cpp \
            MainWindow.cpp
        HEADERS += \
            MainWindow.hpp
        FORMS += \
            MainWindow.ui
        

        main.cpp:

        #include "MainWindow.hpp"
        #include <QApplication>
        int main(int argc, char *argv[])
        {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
        }
        

        MainWindow.hpp

        #ifndef MAINWINDOW_HPP
        #define MAINWINDOW_HPP
        
        #include <QMainWindow>
        
        QT_BEGIN_NAMESPACE
        namespace Ui {
        class MainWindow;
        }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow {
          Q_OBJECT
        public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
        public slots:
          void handleBrowseClicked(bool);
          void handleClearButtonClicked(bool);
          void handleEditingFinished();
          void handleTextChanged(const QString &txt);
        private:
          Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_HPP
        

        MainWindow.cpp:

        #include <QtCore>
        #include <QFileDialog>
        #include <QtDebug>
        #include "MainWindow.hpp"
        #include "ui_MainWindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
        {
          ui->setupUi(this);
          // Connect signals to slots:
          connect(ui->btnBrowse, &QPushButton::clicked,
                  this, &MainWindow::handleBrowseClicked);
          connect(ui->edtInput, &QLineEdit::editingFinished,
                  this, &MainWindow::handleEditingFinished);
          connect(ui->edtInput, &QLineEdit::textChanged,
                  this, &MainWindow::handleTextChanged);
          QList<QAction*> actionList
              = ui->edtInput->findChildren<QAction*>();
        
          // Connects the 'clear' button's QAction to our slot:
          if (!actionList.isEmpty()) {
            connect(actionList.first(), &QAction::triggered,
                    this, &MainWindow::handleClearButtonClicked);
          }
        }
        
        MainWindow::~MainWindow()
        {
          delete ui;
        }
        
        void MainWindow::handleBrowseClicked(bool)
        {
          static const QString msg(__func__);
          qDebug() << msg;
          ui->txtLog->appendPlainText(msg);
        
          const QString fname = QFileDialog::getOpenFileName(
              this,"Select Any File");
          if (!fname.isEmpty()) {
            ui->edtInput->setText(fname);
          }
        }
        
        void MainWindow::handleClearButtonClicked(bool)
        {
          static const QString msg(__func__);
          qDebug() << msg;
          ui->txtLog->appendPlainText(msg);
        
          handleEditingFinished();
        }
        
        void MainWindow::handleEditingFinished()
        {
          QString msg;
          msg = QString(__func__).append("\n\tInput: ").append(ui->edtInput->text());
          qDebug() << msg;
          ui->txtLog->appendPlainText(msg);
        }
        
        void MainWindow::handleTextChanged(const QString &txt)
        {
          QString msg;
          msg = QString(__func__).append("\n\tInput: ").append(txt);
          qDebug() << msg;
          ui->txtLog->appendPlainText(msg);
        }
        

        MainWindow.ui:

        <?xml version="1.0" encoding="UTF-8"?>
        <ui version="4.0">
         <class>MainWindow</class>
         <widget class="QMainWindow" name="MainWindow">
          <property name="geometry">
           <rect>
            <x>0</x>
            <y>0</y>
            <width>669</width>
            <height>329</height>
           </rect>
          </property>
          <property name="windowTitle">
           <string>MainWindow</string>
          </property>
          <widget class="QWidget" name="centralwidget">
           <layout class="QGridLayout" name="gridLayout">
            <item row="0" column="0">
             <layout class="QVBoxLayout" name="verticalLayout_2">
              <item>
               <layout class="QHBoxLayout" name="horizontalLayout">
                <item>
                 <widget class="QLabel" name="lblInput">
                  <property name="text">
                   <string>&amp;File name:</string>
                  </property>
                  <property name="buddy">
                   <cstring>edtInput</cstring>
                  </property>
                 </widget>
                </item>
                <item>
                 <widget class="QLineEdit" name="edtInput">
                  <property name="clearButtonEnabled">
                   <bool>true</bool>
                  </property>
                 </widget>
                </item>
                <item>
                 <widget class="QPushButton" name="btnBrowse">
                  <property name="text">
                   <string>&amp;Browse...</string>
                  </property>
                 </widget>
                </item>
               </layout>
              </item>
              <item>
               <layout class="QVBoxLayout" name="verticalLayout">
                <item>
                 <widget class="QLabel" name="lblLog">
                  <property name="text">
                   <string>&amp;Log:</string>
                  </property>
                  <property name="buddy">
                   <cstring>txtLog</cstring>
                  </property>
                 </widget>
                </item>
                <item>
                 <widget class="QPlainTextEdit" name="txtLog"/>
                </item>
               </layout>
              </item>
             </layout>
            </item>
           </layout>
          </widget>
          <widget class="QMenuBar" name="menubar">
           <property name="geometry">
            <rect>
             <x>0</x>
             <y>0</y>
             <width>669</width>
             <height>23</height>
            </rect>
           </property>
          </widget>
          <widget class="QStatusBar" name="statusbar"/>
         </widget>
         <resources/>
         <connections/>
        </ui>
        

        Screenshot:
        test_line_edit.png

        JonBJ I 2 Replies Last reply
        0
        • R Offline
          R Offline
          Robert Hairgrove
          wrote last edited by
          #4

          I think the problem is that the "clear" button is really only meant to be a convenience for editing, and that one shouldn't rely on this QAction for anything. There is some heavy lifting done only when the editing is finished, though, which I do not want to call every time the text changes.

          1 Reply Last reply
          0
          • R Robert Hairgrove

            @Christian-Ehrlicher Very funny.

            Here is some example code -- a screenshot was taken and attached at the end after the code.
            Built with Qt 6.9.1 and gcc 13 on Ubuntu 24.04.3 LTS.

            NOTE: The slot which is connected to the clear button's QAction calls "handleEditingFinished()", which is where it would seem to be expected that calling "QLineEdit::text()" should return an empty string. However, it only returns the empty string in the "textChanged()" slot.

            Project file (qmake): "Test_LineEdit.pro"

            QT += core gui widgets
            CONFIG += c++17
            SOURCES += \
                main.cpp \
                MainWindow.cpp
            HEADERS += \
                MainWindow.hpp
            FORMS += \
                MainWindow.ui
            

            main.cpp:

            #include "MainWindow.hpp"
            #include <QApplication>
            int main(int argc, char *argv[])
            {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
              return a.exec();
            }
            

            MainWindow.hpp

            #ifndef MAINWINDOW_HPP
            #define MAINWINDOW_HPP
            
            #include <QMainWindow>
            
            QT_BEGIN_NAMESPACE
            namespace Ui {
            class MainWindow;
            }
            QT_END_NAMESPACE
            
            class MainWindow : public QMainWindow {
              Q_OBJECT
            public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
            public slots:
              void handleBrowseClicked(bool);
              void handleClearButtonClicked(bool);
              void handleEditingFinished();
              void handleTextChanged(const QString &txt);
            private:
              Ui::MainWindow *ui;
            };
            #endif // MAINWINDOW_HPP
            

            MainWindow.cpp:

            #include <QtCore>
            #include <QFileDialog>
            #include <QtDebug>
            #include "MainWindow.hpp"
            #include "ui_MainWindow.h"
            
            MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
            {
              ui->setupUi(this);
              // Connect signals to slots:
              connect(ui->btnBrowse, &QPushButton::clicked,
                      this, &MainWindow::handleBrowseClicked);
              connect(ui->edtInput, &QLineEdit::editingFinished,
                      this, &MainWindow::handleEditingFinished);
              connect(ui->edtInput, &QLineEdit::textChanged,
                      this, &MainWindow::handleTextChanged);
              QList<QAction*> actionList
                  = ui->edtInput->findChildren<QAction*>();
            
              // Connects the 'clear' button's QAction to our slot:
              if (!actionList.isEmpty()) {
                connect(actionList.first(), &QAction::triggered,
                        this, &MainWindow::handleClearButtonClicked);
              }
            }
            
            MainWindow::~MainWindow()
            {
              delete ui;
            }
            
            void MainWindow::handleBrowseClicked(bool)
            {
              static const QString msg(__func__);
              qDebug() << msg;
              ui->txtLog->appendPlainText(msg);
            
              const QString fname = QFileDialog::getOpenFileName(
                  this,"Select Any File");
              if (!fname.isEmpty()) {
                ui->edtInput->setText(fname);
              }
            }
            
            void MainWindow::handleClearButtonClicked(bool)
            {
              static const QString msg(__func__);
              qDebug() << msg;
              ui->txtLog->appendPlainText(msg);
            
              handleEditingFinished();
            }
            
            void MainWindow::handleEditingFinished()
            {
              QString msg;
              msg = QString(__func__).append("\n\tInput: ").append(ui->edtInput->text());
              qDebug() << msg;
              ui->txtLog->appendPlainText(msg);
            }
            
            void MainWindow::handleTextChanged(const QString &txt)
            {
              QString msg;
              msg = QString(__func__).append("\n\tInput: ").append(txt);
              qDebug() << msg;
              ui->txtLog->appendPlainText(msg);
            }
            

            MainWindow.ui:

            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
             <class>MainWindow</class>
             <widget class="QMainWindow" name="MainWindow">
              <property name="geometry">
               <rect>
                <x>0</x>
                <y>0</y>
                <width>669</width>
                <height>329</height>
               </rect>
              </property>
              <property name="windowTitle">
               <string>MainWindow</string>
              </property>
              <widget class="QWidget" name="centralwidget">
               <layout class="QGridLayout" name="gridLayout">
                <item row="0" column="0">
                 <layout class="QVBoxLayout" name="verticalLayout_2">
                  <item>
                   <layout class="QHBoxLayout" name="horizontalLayout">
                    <item>
                     <widget class="QLabel" name="lblInput">
                      <property name="text">
                       <string>&amp;File name:</string>
                      </property>
                      <property name="buddy">
                       <cstring>edtInput</cstring>
                      </property>
                     </widget>
                    </item>
                    <item>
                     <widget class="QLineEdit" name="edtInput">
                      <property name="clearButtonEnabled">
                       <bool>true</bool>
                      </property>
                     </widget>
                    </item>
                    <item>
                     <widget class="QPushButton" name="btnBrowse">
                      <property name="text">
                       <string>&amp;Browse...</string>
                      </property>
                     </widget>
                    </item>
                   </layout>
                  </item>
                  <item>
                   <layout class="QVBoxLayout" name="verticalLayout">
                    <item>
                     <widget class="QLabel" name="lblLog">
                      <property name="text">
                       <string>&amp;Log:</string>
                      </property>
                      <property name="buddy">
                       <cstring>txtLog</cstring>
                      </property>
                     </widget>
                    </item>
                    <item>
                     <widget class="QPlainTextEdit" name="txtLog"/>
                    </item>
                   </layout>
                  </item>
                 </layout>
                </item>
               </layout>
              </widget>
              <widget class="QMenuBar" name="menubar">
               <property name="geometry">
                <rect>
                 <x>0</x>
                 <y>0</y>
                 <width>669</width>
                 <height>23</height>
                </rect>
               </property>
              </widget>
              <widget class="QStatusBar" name="statusbar"/>
             </widget>
             <resources/>
             <connections/>
            </ui>
            

            Screenshot:
            test_line_edit.png

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by
            #5

            @Robert-Hairgrove
            Sorry, but I am not seeing/understanding where you say your clear button (clearButtonEnabled) is clearing your QLineEdit at all, which you seem to say it does? I have not tested it but I do not think the button itself clears the text, you have to connect something to do that? And you do not call clear() or setText() to do so? Or are you saying it does clear, but apparently not till after the QAction::triggered() has fired?

            R 1 Reply Last reply
            0
            • JonBJ JonB

              @Robert-Hairgrove
              Sorry, but I am not seeing/understanding where you say your clear button (clearButtonEnabled) is clearing your QLineEdit at all, which you seem to say it does? I have not tested it but I do not think the button itself clears the text, you have to connect something to do that? And you do not call clear() or setText() to do so? Or are you saying it does clear, but apparently not till after the QAction::triggered() has fired?

              R Offline
              R Offline
              Robert Hairgrove
              wrote last edited by Robert Hairgrove
              #6

              @JonB said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

              Or are you saying it does clear, but apparently not till after the QAction::triggered() has fired?

              This is what I meant.

              I think I will just set some kind of Boolean flag when the clear button was clicked and check the state of that flag in the handleEditingFinished() slot. In this case it is safer than relying on the text() function, I think.

              Another idea would be to check the value returned by sender() in the handleEditingFinished() slot. If it is empty, or not from the QLineEdit widget, then it must be from calling it directly from the other slot.

              JonBJ I 2 Replies Last reply
              0
              • R Robert Hairgrove

                @Christian-Ehrlicher Very funny.

                Here is some example code -- a screenshot was taken and attached at the end after the code.
                Built with Qt 6.9.1 and gcc 13 on Ubuntu 24.04.3 LTS.

                NOTE: The slot which is connected to the clear button's QAction calls "handleEditingFinished()", which is where it would seem to be expected that calling "QLineEdit::text()" should return an empty string. However, it only returns the empty string in the "textChanged()" slot.

                Project file (qmake): "Test_LineEdit.pro"

                QT += core gui widgets
                CONFIG += c++17
                SOURCES += \
                    main.cpp \
                    MainWindow.cpp
                HEADERS += \
                    MainWindow.hpp
                FORMS += \
                    MainWindow.ui
                

                main.cpp:

                #include "MainWindow.hpp"
                #include <QApplication>
                int main(int argc, char *argv[])
                {
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
                  return a.exec();
                }
                

                MainWindow.hpp

                #ifndef MAINWINDOW_HPP
                #define MAINWINDOW_HPP
                
                #include <QMainWindow>
                
                QT_BEGIN_NAMESPACE
                namespace Ui {
                class MainWindow;
                }
                QT_END_NAMESPACE
                
                class MainWindow : public QMainWindow {
                  Q_OBJECT
                public:
                  MainWindow(QWidget *parent = nullptr);
                  ~MainWindow();
                public slots:
                  void handleBrowseClicked(bool);
                  void handleClearButtonClicked(bool);
                  void handleEditingFinished();
                  void handleTextChanged(const QString &txt);
                private:
                  Ui::MainWindow *ui;
                };
                #endif // MAINWINDOW_HPP
                

                MainWindow.cpp:

                #include <QtCore>
                #include <QFileDialog>
                #include <QtDebug>
                #include "MainWindow.hpp"
                #include "ui_MainWindow.h"
                
                MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
                {
                  ui->setupUi(this);
                  // Connect signals to slots:
                  connect(ui->btnBrowse, &QPushButton::clicked,
                          this, &MainWindow::handleBrowseClicked);
                  connect(ui->edtInput, &QLineEdit::editingFinished,
                          this, &MainWindow::handleEditingFinished);
                  connect(ui->edtInput, &QLineEdit::textChanged,
                          this, &MainWindow::handleTextChanged);
                  QList<QAction*> actionList
                      = ui->edtInput->findChildren<QAction*>();
                
                  // Connects the 'clear' button's QAction to our slot:
                  if (!actionList.isEmpty()) {
                    connect(actionList.first(), &QAction::triggered,
                            this, &MainWindow::handleClearButtonClicked);
                  }
                }
                
                MainWindow::~MainWindow()
                {
                  delete ui;
                }
                
                void MainWindow::handleBrowseClicked(bool)
                {
                  static const QString msg(__func__);
                  qDebug() << msg;
                  ui->txtLog->appendPlainText(msg);
                
                  const QString fname = QFileDialog::getOpenFileName(
                      this,"Select Any File");
                  if (!fname.isEmpty()) {
                    ui->edtInput->setText(fname);
                  }
                }
                
                void MainWindow::handleClearButtonClicked(bool)
                {
                  static const QString msg(__func__);
                  qDebug() << msg;
                  ui->txtLog->appendPlainText(msg);
                
                  handleEditingFinished();
                }
                
                void MainWindow::handleEditingFinished()
                {
                  QString msg;
                  msg = QString(__func__).append("\n\tInput: ").append(ui->edtInput->text());
                  qDebug() << msg;
                  ui->txtLog->appendPlainText(msg);
                }
                
                void MainWindow::handleTextChanged(const QString &txt)
                {
                  QString msg;
                  msg = QString(__func__).append("\n\tInput: ").append(txt);
                  qDebug() << msg;
                  ui->txtLog->appendPlainText(msg);
                }
                

                MainWindow.ui:

                <?xml version="1.0" encoding="UTF-8"?>
                <ui version="4.0">
                 <class>MainWindow</class>
                 <widget class="QMainWindow" name="MainWindow">
                  <property name="geometry">
                   <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>669</width>
                    <height>329</height>
                   </rect>
                  </property>
                  <property name="windowTitle">
                   <string>MainWindow</string>
                  </property>
                  <widget class="QWidget" name="centralwidget">
                   <layout class="QGridLayout" name="gridLayout">
                    <item row="0" column="0">
                     <layout class="QVBoxLayout" name="verticalLayout_2">
                      <item>
                       <layout class="QHBoxLayout" name="horizontalLayout">
                        <item>
                         <widget class="QLabel" name="lblInput">
                          <property name="text">
                           <string>&amp;File name:</string>
                          </property>
                          <property name="buddy">
                           <cstring>edtInput</cstring>
                          </property>
                         </widget>
                        </item>
                        <item>
                         <widget class="QLineEdit" name="edtInput">
                          <property name="clearButtonEnabled">
                           <bool>true</bool>
                          </property>
                         </widget>
                        </item>
                        <item>
                         <widget class="QPushButton" name="btnBrowse">
                          <property name="text">
                           <string>&amp;Browse...</string>
                          </property>
                         </widget>
                        </item>
                       </layout>
                      </item>
                      <item>
                       <layout class="QVBoxLayout" name="verticalLayout">
                        <item>
                         <widget class="QLabel" name="lblLog">
                          <property name="text">
                           <string>&amp;Log:</string>
                          </property>
                          <property name="buddy">
                           <cstring>txtLog</cstring>
                          </property>
                         </widget>
                        </item>
                        <item>
                         <widget class="QPlainTextEdit" name="txtLog"/>
                        </item>
                       </layout>
                      </item>
                     </layout>
                    </item>
                   </layout>
                  </widget>
                  <widget class="QMenuBar" name="menubar">
                   <property name="geometry">
                    <rect>
                     <x>0</x>
                     <y>0</y>
                     <width>669</width>
                     <height>23</height>
                    </rect>
                   </property>
                  </widget>
                  <widget class="QStatusBar" name="statusbar"/>
                 </widget>
                 <resources/>
                 <connections/>
                </ui>
                

                Screenshot:
                test_line_edit.png

                I Offline
                I Offline
                IgKh
                wrote last edited by
                #7

                @Robert-Hairgrove said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                QList<QAction*> actionList
                      = ui->edtInput->findChildren<QAction*>();
                
                  // Connects the 'clear' button's QAction to our slot:
                  if (!actionList.isEmpty()) {
                    connect(actionList.first(), &QAction::triggered,
                            this, &MainWindow::handleClearButtonClicked);
                  }
                

                The moment you are doing something like this, you are making assumptions about undocumented and non-guaranteed internal implementation details of the QLineEdit. I.e that the clear button added by setClearButtonEnabled is even tied to a QAction in the object ownership hierarchy of the QLineEdit. Even if something works like this today, even a patch version of Qt can change it (they usually don't, but minor bug fixes can and do have subtle ripple effects on undocumented behavior).

                In this case, the issue probably stems from the fact that both you and QLineEdit internals have connected a slot to the same signal, and you have no control over which is going to be called first. Yours was probably first, so the text is not yet cleared.

                If you need fine grained control, don't use the built-in action, create one of your own using the documented API which you can manage at will.

                1 Reply Last reply
                0
                • R Robert Hairgrove

                  @JonB said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                  Or are you saying it does clear, but apparently not till after the QAction::triggered() has fired?

                  This is what I meant.

                  I think I will just set some kind of Boolean flag when the clear button was clicked and check the state of that flag in the handleEditingFinished() slot. In this case it is safer than relying on the text() function, I think.

                  Another idea would be to check the value returned by sender() in the handleEditingFinished() slot. If it is empty, or not from the QLineEdit widget, then it must be from calling it directly from the other slot.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote last edited by
                  #8

                  @Robert-Hairgrove
                  And that may be fine.
                  You could also --- and it would tell you what is going on even if you don't stick with it --- make the button call handleClearButtonClicked() delayed on a QTimer::singleShot() of a few milliseconds. If that works it tells you about when the text actually gets cleared. It may mean handleClearButtonClicked() is called later than editingFinished() now, I don't know if that matters to you.

                  1 Reply Last reply
                  0
                  • R Robert Hairgrove

                    @JonB said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                    Or are you saying it does clear, but apparently not till after the QAction::triggered() has fired?

                    This is what I meant.

                    I think I will just set some kind of Boolean flag when the clear button was clicked and check the state of that flag in the handleEditingFinished() slot. In this case it is safer than relying on the text() function, I think.

                    Another idea would be to check the value returned by sender() in the handleEditingFinished() slot. If it is empty, or not from the QLineEdit widget, then it must be from calling it directly from the other slot.

                    I Offline
                    I Offline
                    IgKh
                    wrote last edited by
                    #9

                    @Robert-Hairgrove said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                    Another idea would be to check the value returned by sender() in the handleEditingFinished() slot. If it is empty, or not from the QLineEdit widget, then it must be from calling it directly from the other slot.

                    Few notes on that - the first is that it won't work that way. QObject::sender() isn't magic, and has no way of knowing that you called it from a function that is not directly the slot. The second is that you never know who the actual sending object is, and can't assume that it is the QLineEdit directly (and not a sub-object of it, or something else entirely).

                    And in general - and for anyone else reading - this can't be emphasized enough: The QObject protected methods like sender(), receivers(), connectNotify() and similar are blunt tools for very very specific situations that rarely truly arise in applications. Their use is almost certainly a code smell - and indicates that the design needs to be changed to reduce coupling.

                    JonBJ 1 Reply Last reply
                    0
                    • I IgKh

                      @Robert-Hairgrove said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                      Another idea would be to check the value returned by sender() in the handleEditingFinished() slot. If it is empty, or not from the QLineEdit widget, then it must be from calling it directly from the other slot.

                      Few notes on that - the first is that it won't work that way. QObject::sender() isn't magic, and has no way of knowing that you called it from a function that is not directly the slot. The second is that you never know who the actual sending object is, and can't assume that it is the QLineEdit directly (and not a sub-object of it, or something else entirely).

                      And in general - and for anyone else reading - this can't be emphasized enough: The QObject protected methods like sender(), receivers(), connectNotify() and similar are blunt tools for very very specific situations that rarely truly arise in applications. Their use is almost certainly a code smell - and indicates that the design needs to be changed to reduce coupling.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote last edited by
                      #10

                      @IgKh said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                      Few notes on that - the first is that it won't work that way. QObject::sender() isn't magic, and has no way of knowing that you called it from a function that is not directly the slot. The second is that you never know who the actual sending object is, and can't assume that it is the QLineEdit directly (and not a sub-object of it, or something else entirely).

                      @Robert-Hairgrove said in QLineEdit does not clear its &#x60;text()&#x60; after the clear button is clicked:

                      Another idea would be to check the value returned by sender() in the handleEditingFinished() slot. If it is empty, or not from the QLineEdit widget, then it must be from calling it directly from the other slot.

                      Set the connect() on editingFinished to call a lambda, passing on ui->edtInput explicitly to your slot rather than trying to use sender().

                      1 Reply Last reply
                      0
                      • R Robert Hairgrove has marked this topic as solved

                      • Login

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