-1

I created a signal, which is emitted when the user input something(a number) in a qlineedit field, the signal is emitted with a parametre(the number that the user just type in the field). And i would like to use that parametre as a regular number(in a variable). Im trying to add that signal parametre to another number, and i had an error "s1 is not declared". Here is my class in the .h file and his implementation in the .cpp file

the.h file

class fenetre: public QWidget
{
Q_OBJECT
public:
    fenetre();

public slots:
    void calc();
    void clearinput();

signals:
    void thesecond(int s1);

private:
    QPushButton *button1;
    QPushButton *button2;
    QPushButton *button3;
    QPushButton *button4;
    QPushButton *button5;
    QPushButton *result0;
    QPushButton *clear;
    QLineEdit *input1;
    //QLineEdit *inputsqrt;
    //QLineEdit *input2;
    //QLineEdit *result;
    //QLineEdit *square;
};

the.cpp file

QObject::connect(button1,SIGNAL(clicked()),this,SLOT(calc()));
    //QObject::connect(button2,SIGNAL(clicked()),this,SLOT(calc()));
    //QObject::connect(button3,SIGNAL(clicked()),this,SLOT(calc()));
    //QObject::connect(button4,SIGNAL(clicked()),this,SLOT(calc()));
    //QObject::connect(button5,SIGNAL(clicked()),this,SLOT(calc()));
    //QObject::connect(clear,SIGNAL(clicked()),this,SLOT(clearinput()));

    //QObject::connect(result0,SIGNAL(clicked()),this,SLOT(calc()));

    QObject::connect(result0,SIGNAL(thesecond(int)),this,SLOT(calc()));


}





void fenetre::calc()
{
    QString s=input1->text();
    bool ok;
   if(!input1->text().isEmpty())
   {
       int s1=s.toInt(&ok,10);
       emit thesecond(s1);
       input1->clear();
   }

   QObject* obj=sender();
   if(obj==result0)
   {
       int s2=s.toInt(&ok,10);
       int A=s2+thesecond(s1);
        input1->clear();
        QString c=QString::number(A);
        input1->setText(c);
   }
}
Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
Barbell
  • 194
  • 1
  • 6
  • 19

1 Answers1

0

Your code doesn't make sense.

calc() doesn't pass a parameter. There is no s1 for you to use in calc(). There is one you declare in the first if statement, but since it is declared in the block, it is out of scope after the block. Then you are using the signal as a function with a return type, which is kind of odd, considering the signal itself is declared with a void return type. Signals in Qt can return values, but that's extremely rarely used, and it will have to actually return a value for that to work.

QPushButton doesn't have a thesecond(int s1) signal. The last connection statement fails. You don't even check whether it does.

If we assume that in the last connection you meant to say this instead of result0, then what you end up with is a button connecting to calc() which emits a signal, connected to... calc() again!

All in all, your code is conceptually entirely wrong, and you don't seem to know what you are doing. Also, you might want to learn a bit on proper coding conventions, because that code is atrocious.

I suggest you edit the question and carefully explain what you actually want to happen, because the way it is right now, your intent is a mystery. Judging by your other questions, it definitely looks like you are getting ahead of yourself, go back and do more learning before you try to use it. You will have a very bad time with programming if you don't have a clue what you are doing. There is a huge difference between making occasional mistakes and having no idea what you are doing, the former is the scope of this site, the second isn't.

dtech
  • 47,916
  • 17
  • 112
  • 190