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. Cyclic call between 2 QLineEdit and signals
QtWS25 Last Chance

Cyclic call between 2 QLineEdit and signals

Scheduled Pinned Locked Moved Solved General and Desktop
cyclicsignalinfiniteloop
8 Posts 4 Posters 2.5k 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.
  • M Offline
    M Offline
    MaxiARG
    wrote on 8 Sept 2017, 20:06 last edited by
    #1

    Hi guys, first post here.
    I just did a simple km/h to m/s conversor. 2 QLineEdit triggered with textChanged. "calculadora" just means calculator. The problem is that when i write something in the left box, it triggers another event in the right box, then enters an infinite loop of signal calls. How should i avoid this?

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        calculadora= new Calculadora(ui->lineEdit_Kms, ui->lineEdit_ms);
    
        QObject::connect(ui->lineEdit_Kms,SIGNAL(textChanged(QString)),
                         calculadora, SLOT(kms2ms(QString))
                         );
        QObject::connect(ui->lineEdit_ms,SIGNAL(textChanged(QString)),
                         calculadora, SLOT(ms2kms(QString))
                         );
    }
    
    Calculadora::Calculadora(QLineEdit *_kms,QLineEdit *_ms){
        kms=_kms;
        ms=_ms;
    }
    
    void Calculadora::kms2ms(QString val)
    {
       double res= val.toDouble()*3.6f;
       QString texto= QString::number(res);
    
       ms->setText("");
       ms->insert(texto);
    }
    
    void Calculadora::ms2kms(QString val)
    {
         float div=0.27f;
        double res= val.toDouble()*div;
        QString texto= QString::number(res);
        kms->setText("");
        kms->insert(texto);
    }
    
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 8 Sept 2017, 20:36 last edited by mpergand 9 Aug 2017, 20:46
      #2

      Hi,

      That's normal, when you do setText() on the second LineEdit, the signal is emitted.

      Two options:

      • call blockSignals(true) on the second LineEdit before setText and again with false. (never used this method, hopes it's work ...)
      • use a Button and update the LineEdits when the user clicks it.

      [EDIT] A better idea would be to use returnPressed() signal.

      M 1 Reply Last reply 9 Sept 2017, 02:55
      0
      • A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 8 Sept 2017, 20:54 last edited by
        #3

        @MaxiARG: You can just use the textEdited(QString) signal instead textChanged and it should work.

        Qt has to stay free or it will die.

        M 1 Reply Last reply 9 Sept 2017, 03:04
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Sept 2017, 21:38 last edited by
          #4

          Hi and welcome to devnet,

          To add to my fellows, why do you call setText and then insert ? Why not directly setText(texto) ?

          On a side note, you should use const QString &val as slot parameters. That will avoid useless copies.

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

          M 1 Reply Last reply 9 Sept 2017, 02:22
          0
          • S SGaist
            8 Sept 2017, 21:38

            Hi and welcome to devnet,

            To add to my fellows, why do you call setText and then insert ? Why not directly setText(texto) ?

            On a side note, you should use const QString &val as slot parameters. That will avoid useless copies.

            M Offline
            M Offline
            MaxiARG
            wrote on 9 Sept 2017, 02:22 last edited by
            #5

            @SGaist Thanks SGaist! it was my first 24 hours of QT so i am like a big sponge absorving lots of knowledge like yours. i really apreciate your sugestion. I will use const QString &val from now on i just wanted to keep it simple, but i will investigate ir further. At the other hand, when i tried to set a double or a float value to the QEditLine, it printed "inf" or really big numbers and thats why i splited a method that i could have done in 1 line, in 5 lines. this part was tricky.
            After a couple of minutes i succed but i left the code just like that, without properly refactoring it hehe. Many thanks for your reply!

               double res= val.toDouble()*3.6f;
               QString texto= QString::number(res);
               ms->setText("");
               ms->insert(texto);
            
            1 Reply Last reply
            0
            • M mpergand
              8 Sept 2017, 20:36

              Hi,

              That's normal, when you do setText() on the second LineEdit, the signal is emitted.

              Two options:

              • call blockSignals(true) on the second LineEdit before setText and again with false. (never used this method, hopes it's work ...)
              • use a Button and update the LineEdits when the user clicks it.

              [EDIT] A better idea would be to use returnPressed() signal.

              M Offline
              M Offline
              MaxiARG
              wrote on 9 Sept 2017, 02:55 last edited by
              #6

              @mpergand Many thanks mpergand! my first 24 hours with QT was a succed! i will try both options just to check how it works! many thanks again!

              1 Reply Last reply
              0
              • A aha_1980
                8 Sept 2017, 20:54

                @MaxiARG: You can just use the textEdited(QString) signal instead textChanged and it should work.

                M Offline
                M Offline
                MaxiARG
                wrote on 9 Sept 2017, 03:04 last edited by
                #7

                @aha_1980 Many thanks sir! textEdited together with setText was a quick solution. i had to replace insert for setText. aparently insert emmits textEdited so i had to replace that sentence too. Many thanks!

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Sept 2017, 19:53 last edited by
                  #8

                  Again, remove the insert line and change the setText call to setText(texto). You'll avoid useless operations and your code will be cleaner.

                  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
                  0

                  1/8

                  8 Sept 2017, 20:06

                  • 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