Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cyclic call between 2 QLineEdit and signals



  • 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);
    }
    
    
    


  • 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.


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    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.



  • @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);
    


  • @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!



  • @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!


  • Lifetime Qt Champion

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