Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to identify which lineEdit has changed in the ui?
Forum Updated to NodeBB v4.3 + New Features

How to identify which lineEdit has changed in the ui?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 5 Posters 2.2k Views 2 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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on last edited by
    #1

    I have a ui with 30 lineEdits,When user changes Nth lineEdit value I want to set a bool value_changed_lineEdit_N as true.
    I tried
    connect(ui->lineEdit,SIGNAL(textChanged(QString),this,value_changed_lineEdit=true);

    Is there any way to do this?Please suggest.

    aha_1980A 1 Reply Last reply
    0
    • K kishore_hemmady

      I have a ui with 30 lineEdits,When user changes Nth lineEdit value I want to set a bool value_changed_lineEdit_N as true.
      I tried
      connect(ui->lineEdit,SIGNAL(textChanged(QString),this,value_changed_lineEdit=true);

      Is there any way to do this?Please suggest.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @kishore_hemmady,

      you can try (untested):

      connect(u->lineEdit, &QLineEdit::textChanged, this, [this]() {
        value_changed_lineEdit=true;
      });
      

      You can make the whole thing more easy by creating the lineEdits in code and use QSignalMapper.

      Your whole problem should then be solved with a few lines of code.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        Slot has to be a function or a lambda expression. Writing "live code" like this won't work.

        One approach would be to store your line edits (pointers to them) in a QVector in the same order that your N numbering has. Then you will be able to check which line edit has fired the signal (use sender() in your slot for that) is the Nth in your vector - and that will give you the "N" number.

        Or, instead of declaring 30 boolean members, do this using QHash<QLineEdit*, bool> and store the modification flag in the hash.

        (Z(:^

        1 Reply Last reply
        2
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          To complete the hat-trick,
          you can also subclass QLineEdit and make a new signal with an identifier:

          class myLineEdit : public QLineEdit
          {
              Q_OBJECT
          public:
              explicit cLineEdit(QWidget * parent = 0, int Id) :
                  QLineEdit(parent), m_Id(id){
                  connect(this, SIGNAL(textChanged(QString)), this, SLOT(sendMySignal(QString)) );
             }
          
          private slots:
              void sendMySignal(QString &str){
                   emit mySignal(str,m_Id);
              }
          
          signals:
             void mySignal(QString,int);
          
          private:
            int m_Id;
          };
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          aha_1980A 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            To complete the hat-trick,
            you can also subclass QLineEdit and make a new signal with an identifier:

            class myLineEdit : public QLineEdit
            {
                Q_OBJECT
            public:
                explicit cLineEdit(QWidget * parent = 0, int Id) :
                    QLineEdit(parent), m_Id(id){
                    connect(this, SIGNAL(textChanged(QString)), this, SLOT(sendMySignal(QString)) );
               }
            
            private slots:
                void sendMySignal(QString &str){
                     emit mySignal(str,m_Id);
                }
            
            signals:
               void mySignal(QString,int);
            
            private:
              int m_Id;
            };
            
            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @J.Hilk: Cool. Haven't though about this.

            @kishore_hemmady: If you like to go @J-Hilk's way, you can use the "promote" feature in Qt Designer to change the QLineEdit's to MyLineEdit's

            Qt has to stay free or it will die.

            1 Reply Last reply
            2
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Just one small addition to @J-Hilk suggestion: put the parent parameter as the last one. That's the common way to write constructors with multiple parameters for QObject/QWidget derived classes.

              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
              3

              • Login

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