Results 1 to 15 of 15

Thread: Will I learn to implement custom SIGNALS?

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Will I learn to implement custom SIGNALS?

    Reading examples and documentation I start to believe that making a custom SIGNAL is not a difficult task. Trying to do it though I realize that I cannot do it!!

    Here's a simple example, that if I understand it I will be able to make more custom signals.

    1. a line edit for number A
    2. a line edit for number B
    3. a line edit to present the result of the Multiplication


    Here is the corresponding code:


    Qt Code:
    1. MyDialog::MyDialog(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4.  
    5. QLineEdit *numberA = new QLineEdit;
    6. QLineEdit *numberB = new QLineEdit;
    7.  
    8. result = new QLineEdit;
    9.  
    10. numberA->setText("1.0");
    11. numberB->setText("1.0");
    12. result->setText("1.0");
    13.  
    14.  
    15.  
    16. vboxlayout = new QVBoxLayout(this);
    17. vboxlayout->addWidget(numberA);
    18. vboxlayout->addWidget(new QLabel("x"));
    19. vboxlayout->addWidget(numberB);
    20. vboxlayout->addWidget(new QLabel("="));
    21. vboxlayout->addWidget(result);
    22.  
    23. setLayout(vboxlayout);
    24. }
    25.  
    26.  
    27. void MyDialog::showMultiplication(QString Astring,QString Bstring)
    28. {
    29. double A,B,Res;
    30. QString ResString;
    31.  
    32.  
    33. A= Astring.toDouble();
    34. B= Bstring.toDouble();
    35.  
    36.  
    37. Res = A*B;
    38.  
    39. ResString.setNum(Res);
    40.  
    41. result->setText(ResString);
    42. }
    To copy to clipboard, switch view to plain text mode 

    QUESTION:
    When the user inserts a new number (anywhere A or B) the multiplication result should be shown directly in the result line edit. The SLOT is obliously the showMultiplication(QString Astring,QString Bstring).

    The SIGNAL should be something like
    sendNewNumbersForMultiplication(QString ,QString )

    BUT, I really cannot understand how to implement it!!
    Any help?

  2. #2
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Will I learn to implement custom SIGNALS?

    Maybe I miss-understand you?

    But don't you just want to declare a signal in your header file. Then emit that signal when you get a signal (whichever you choose) from the Line edit object?

    Like in your header:
    Qt Code:
    1. signals:
    2. void sendNewNumbersForMultiplication(QString ,QString );
    To copy to clipboard, switch view to plain text mode 

    Is this what you are trying to do?

  3. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    yes, of course, I am trying to make the scenario described above to work. The user writes in a number and the system writes automatically the multiplication result in the result line edit.
    The declaration of the SIGNAL will then be as you said:
    Qt Code:
    1. signals:
    2. void sendNewNumbersForMultiplication(QString ,QString );
    To copy to clipboard, switch view to plain text mode 

    But, what will I write into the implementation of the function in the .cpp file?

    Qt Code:
    1. void MyDialog::sendNewNumbersForMultiplication(QString ,QString )
    2. {
    3.  
    4. ??
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Will I learn to implement custom SIGNALS?

    No, you don't implement it, you emit it.

    Qt Code:
    1. emit sendNewNumbersForMultiplication(numberA.text(),numberB.text());
    To copy to clipboard, switch view to plain text mode 

    Then you just connect your signal to a slot to display it. You implement the slot.

    You do this from within a function in your class though. I'd guess, in your case from a slot connected to a signal coming from a QLineEdit object.

  5. #5
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    wait, because this is exactly the point I am getting confused!

    QUESTION
    Qt Code:
    1. emit sendNewNumbersForMultiplication(numberA.text(),numberB.text());
    To copy to clipboard, switch view to plain text mode 
    where does this line go into?



    You implement the slot.
    I think the slot is this:
    Qt Code:
    1. void MyDialog::showMultiplication(QString Astring,QString Bstring)
    2. {
    3. double A,B,Res;
    4. QString ResString;
    5.  
    6. A= Astring.toDouble();
    7. B= Bstring.toDouble();
    8.  
    9. Res = A*B;
    10. ResString.setNum(Res);
    11. result->setText(ResString);
    12. }
    To copy to clipboard, switch view to plain text mode 




    Then you just connect your signal to a slot to display it.
    ok, I think this is the connection

    Qt Code:
    1. connect(this, SIGNAL( sendNewNumbersForMultiplication(QString,QString ),this, SLOT(showMultiplication(QString ,QString )) );
    To copy to clipboard, switch view to plain text mode 




    in your case from a slot connected to a signal coming from a QLineEdit object.
    I thought the SIGNAL was the sendNewNumbersForMultiplication(QString,QString ) function

  6. #6
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Will I learn to implement custom SIGNALS?

    But when do you want your signal to be emitted?
    Who is receiving the signal?

    Looks to me you are doing all this within the one object? I'm not sure why, in this case you need to emit a signal, unless another object is going to listen for this signal...

    In your case, just declare the QLineEdit objects in the header file and access them directly...

  7. #7
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    No need to create your own signal.

    In MyDialog::MyDialog(QWidget *parent):
    Qt Code:
    1. connect(numberA, SIGNAL(textChanged(QString)), this, SLOT(multiply(QString)));
    2. connect(numberB, SIGNAL(textChanged(QString)), this, SLOT(multiply(QString)));
    To copy to clipboard, switch view to plain text mode 

    In MyDialog::multiply(QString):
    Qt Code:
    1. A= numberA->text().toDouble();
    2. B= numberB->text().toDouble();
    To copy to clipboard, switch view to plain text mode 

    Sorry, don't have any time to test, but you get the picture, I think...?

  8. #8
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    @boudie:
    Qt Code:
    1. connect(numberA, SIGNAL(textChanged(QString)), this, SLOT(multiply(QString)));
    2. connect(numberB, SIGNAL(textChanged(QString)), this, SLOT(multiply(QString)));
    To copy to clipboard, switch view to plain text mode 


    I don't want to use the built-in signals textChanged(QString) because the actual custom SIGNAL I want to implement is much more complicated than this simple example.
    Anyway, if you notice carefully you'll see that the MyDialog::multiply(QString) does not know the value of the other QString. So, it will not work. Obviously I need to create a SIGNAL which sends two QStrings at a time:

    Qt Code:
    1. sendNewNumbersForMultiplication(QString,QString )
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    To broadcast a signal, you use the emit() statement, as noted above. You define the arguments it will send in your header file, also as noted above. At the point where you emit the signal, you provide it with the values of the arguments you want it to carry, which has been noted above.

  10. The following user says thank you to SixDegrees for this useful post:

    fatecasino (4th March 2011)

  11. #10
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    To broadcast a signal, you use the emit() statement, as noted above. You define the arguments it will send in your header file, also as noted above. At the point where you emit the signal, you provide it with the values of the arguments you want it to carry, which has been noted above.
    can you translate this with the implementation of this SIGNAL?
    Qt Code:
    1. sendNewNumbersForMultiplication(QString,QString )
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Will I learn to implement custom SIGNALS?

    Quote Originally Posted by fatecasino View Post
    @boudie:
    Anyway, if you notice carefully you'll see that the MyDialog::multiply(QString) does not know the value of the other QString. So, it will not work. Obviously I need to create a SIGNAL which sends two QStrings at a time:

    Qt Code:
    1. sendNewNumbersForMultiplication(QString,QString )
    To copy to clipboard, switch view to plain text mode 
    As I notice carefully in your previous posts, both the QLineEdits and the slot are in the same class. So the slot has access to both QLineEdits and their values.

  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Will I learn to implement custom SIGNALS?

    Your reason for a signal does not make sense. A signal is emitted because some action has occured. If you wish to multiply on a button being clicked, the signal will come from the button. If you wish to multiple on the text in an edit box being changed, then the signal will come from the edit box.

    Now, if you wanted a signal which is emitted if the multiplied number is over 1000, then it would be a custom signal. In which case, you would put the definition of the signal in your header file, you would put the 'connect' statement in the class with the slot and emit the signal from your showMultiplication slot. Note that signals do not have implementations, only definitions.

  14. #13
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Will I learn to implement custom SIGNALS?

    Ok so as I understand it you are simply asking how to create a signal with two parameters and how to trigger it when the value of one of the input boxes change?

    in your class definition you define signals like this
    Qt Code:
    1. class myObject : public QWidget
    2. {
    3. Q_OBJECT
    4. signals:
    5. void multiply(const QString& a, const QString& b);
    6.  
    7. private slots:
    8. void onVariableChanged();
    9. void doMultiplication(const QString& a, const QString& b);
    10. ...
    11. };
    To copy to clipboard, switch view to plain text mode 

    then connect the signals and slots
    Qt Code:
    1. connect(numberA, SIGNAL(textChanged(const QString&)), this, SLOT(onVariableChanged()));
    2. connect(numberB, SIGNAL(textChanged(const QString&)), this, SLOT(onVariableChanged()));
    3. connect(this, SIGNAL(multiply(const QString&, const QString&), this, SLOT(doMultiplication(const QString&, const QString&));
    To copy to clipboard, switch view to plain text mode 

    We use the text changed signal to trigger the creation of our more "complicated" multiplication signal and then emit it

    Qt Code:
    1. void MyObject::onVariableChanged()
    2. {
    3. //Query any data we want here and send a new signal
    4. emit multiply(numberA->text(), numberB->text());
    5. }
    6.  
    7. void MyObject::doMultiplication(const QString& a, const QString& b)
    8. {
    9. double x = a.toDouble() * b.toDouble();
    10. result->setText(QString::number(x));
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Berryblue031; 8th March 2011 at 11:59. Reason: updated signal name

  15. #14
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Will I learn to implement custom SIGNALS?

    Berryblue031 post
    Ok, but here is a little confusion - you declare doMultiplication as a slot, and then use it as if it was a signal (with emit).
    As noted in boudie's post, you have access to both lineEdits, so there is no need to send a signal in order to do multiplication - connect textChanged signal to onVariableChanged slot, and simply call multiplication method with values of both lineEdits:

    Qt Code:
    1. class myObject : public QWidget
    2. {
    3. Q_OBJECT
    4. private slots:
    5. void onVariableChanged();
    6. void doMultiplication(const QString& a, const QString& b);
    7. ...
    8. };
    9.  
    10. // ...
    11. connect(numberA, SIGNAL(textChanged(const QString&)), this, SLOT(onVariableChanged()));
    12. connect(numberB, SIGNAL(textChanged(const QString&)), this, SLOT(onVariableChanged()));
    13. //...
    14.  
    15. void MyObject::onVariableChanged(){
    16. this->doMultiplication(numberA->text(), numberB->text());
    17. }
    To copy to clipboard, switch view to plain text mode 
    There's no need to complicate such simple task

  16. #15
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Will I learn to implement custom SIGNALS?

    Quote Originally Posted by stampede View Post
    Ok, but here is a little confusion - you declare doMultiplication as a slot, and then use it as if it was a signal (with emit).
    Ahh you are right that is a typo on my part, I will fix it.

    Quote Originally Posted by fatecasino View Post
    Reading examples and documentation I start to believe that making a custom SIGNAL is not a difficult task. Trying to do it though I realize that I cannot do it!! Here's a simple example, that if I understand it I will be able to make more custom signals.

    The op's problem was in general that he didn't understand how to implement and emit a custom signal, the scenario he posted wasn't really a scenario where he absolutely needed to use signals slots but there is nothing wrong with doing so and it's a good enough example for learning how to emit a signal. Telling him how to solve the problem without using signals kind of defeats the purpose.
    Last edited by Berryblue031; 8th March 2011 at 12:07.

Similar Threads

  1. Signals not going through to custom class.
    By GunBlade in forum Qt Programming
    Replies: 3
    Last Post: 15th August 2010, 12:50
  2. Custom Signals
    By hazardpeter in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 12:09
  3. custom signals
    By talk2amulya in forum Qt Programming
    Replies: 9
    Last Post: 20th February 2009, 08:55
  4. Replies: 2
    Last Post: 19th February 2009, 19:37
  5. Custom signals?
    By godot in forum Newbie
    Replies: 7
    Last Post: 14th January 2008, 19:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.