Results 1 to 6 of 6

Thread: There are no connection with a Button

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default There are no connection with a Button

    Hello!

    If I click on Button nothing is occur.

    6i.png

    I created the project in NetBeans with this tutorial: https://netbeans.org/kb/docs/cnd/qt-applications.html

    Summa.h
    Qt Code:
    1. /*
    2.  * File: Summa.h
    3.  * Author: Ivan
    4.  *
    5.  * Created on April 19, 2013, 6:28 AM
    6.  */
    7.  
    8. #ifndef SUMMA_H
    9. #define SUMMA_H
    10.  
    11. class Summa {
    12. public:
    13. Summa(){m_summa = 0;}
    14. double calcSumma(double a, double b){return a+b;}
    15. double getSumma() {return m_summa;}
    16. private:
    17. double m_summa;
    18. };
    19.  
    20. #endif /* SUMMA_H */
    To copy to clipboard, switch view to plain text mode 

    SummaDialog.h
    Qt Code:
    1. /*
    2.  * File: SummaDialog.h
    3.  * Author: Ivan
    4.  *
    5.  * Created on April 19, 2013, 6:03 AM
    6.  */
    7.  
    8. #ifndef _SUMMADIALOG_H
    9. #define _SUMMADIALOG_H
    10.  
    11. #include "ui_SummaDialog.h"
    12.  
    13. class SummaDialog : public QDialog {
    14. Q_OBJECT
    15. public:
    16. SummaDialog();
    17. virtual ~SummaDialog();
    18. public slots:
    19. void showResult(const double& result);
    20. private:
    21. Ui::SummaDialog widget;
    22. };
    23.  
    24. #endif /* _SUMMADIALOG_H */
    To copy to clipboard, switch view to plain text mode 

    SummaDialog.cpp
    Qt Code:
    1. /*
    2.  * File: SummaDialog.cpp
    3.  * Author: Ivan
    4.  *
    5.  * Created on April 19, 2013, 6:03 AM
    6.  */
    7.  
    8. #include "SummaDialog.h"
    9.  
    10. SummaDialog::SummaDialog() {
    11. widget.setupUi(this);
    12. connect(widget.pushButton, SIGNAL(clicked(const double&)),
    13. this, SLOT(showResult(const double&)));
    14. }
    15.  
    16. SummaDialog::~SummaDialog() {
    17. }
    18.  
    19. void SummaDialog::showResult(const double& result) {
    20. widget.resultEdit->setText(QString::number(result));
    21. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. /*
    2.  * File: main.cpp
    3.  * Author: Ivan
    4.  *
    5.  * Created on April 19, 2013, 6:02 AM
    6.  */
    7.  
    8. #include <QtGui/QApplication>
    9. #include "SummaDialog.h"
    10. #include "Summa.h"
    11.  
    12. int main(int argc, char *argv[]) {
    13. // initialize resources, if needed
    14. // Q_INIT_RESOURCE(resfile);
    15.  
    16. QApplication app(argc, argv);
    17.  
    18. SummaDialog sd;
    19. sd.show();
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: There are no connection with a Button

    read your debug output. This connection will fail:

    connect(widget.pushButton, SIGNAL(clicked(const double&)), this, SLOT(showResult(const double&)));

    There is no such signal on a pushbutton!

    Think about this - how does a button know what double you are talking about?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default Re: There are no connection with a Button

    What I must to write?

    OutPut
    Process is started in an external terminal ...
    It does not work:
    Qt Code:
    1. SummaDialog::SummaDialog() {
    2. widget.setupUi(this);
    3. connect(widget.pushButton, SIGNAL(clicked()),
    4. this, SLOT(showResult(const double&)));
    5. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: There are no connection with a Button

    I say again,

    Think about this - how does a button know what double you are talking about?

    Qt Code:
    1. connect(widget.pushButton, SIGNAL(clicked()),
    2. this, SLOT(showResult(const double&)));
    To copy to clipboard, switch view to plain text mode 
    How can this work? where does that double come from? It can only come from thin air - therefore this is not allowed. You will see in your console window at runtime that this connection fails. You cannot connect a signal with N arguments to a slot with more than N arguments. Logically this does not make sense!

    What you need to do is connect clicked() to a slot that reads the ui values and emits another signal with those value(s).
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. The following user says thank you to amleto for this useful post:

    8Observer8 (20th April 2013)

  6. #5
    Join Date
    Nov 2012
    Posts
    232
    Platforms
    Windows Android
    Thanks
    118
    Thanked 18 Times in 10 Posts

    Default Re: There are no connection with a Button

    amleto, thank you very much!

    Qt Code:
    1. /*
    2.  * File: SummaDialog.cpp
    3.  * Author: Ivan
    4.  *
    5.  * Created on April 19, 2013, 6:03 AM
    6.  */
    7.  
    8. #include "SummaDialog.h"
    9. #include "Summa.h"
    10.  
    11. SummaDialog::SummaDialog() {
    12. widget.setupUi(this);
    13. connect(widget.pushButton, SIGNAL(clicked()),
    14. this, SLOT(calcResultSLOT()));
    15. }
    16.  
    17. void SummaDialog::calcResultSLOT() {
    18. // Take input data
    19. double a = widget.aDSpinBox->value();
    20. double b = widget.bDSpinBox->value();
    21.  
    22. // Calc the sum
    23. Summa s;
    24. double result = s.calcSumma(a, b);
    25.  
    26. // Show the result
    27. widget.resultEdit->setText(QString::number(result));
    28. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: There are no connection with a Button

    That's right!

    If you want to you can emit a signal with the result at the end of calcResultSLOT() to let other widgets receive the result.
    Qt Code:
    1. void SummaDialog::calcResultSLOT() {
    2. // Take input data
    3. double a = widget.aDSpinBox->value();
    4. double b = widget.bDSpinBox->value();
    5.  
    6. // Calc the sum
    7. Summa s;
    8. double result = s.calcSumma(a, b);
    9.  
    10. // Show the result
    11. widget.resultEdit->setText(QString::number(result));
    12. emit resultSIGNAL(result); // add signal to your header as well -- void resultSignal(double result);
    13. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2011, 19:18
  2. Replies: 2
    Last Post: 26th April 2011, 11:44
  3. Replies: 6
    Last Post: 21st August 2010, 21:09
  4. Replies: 1
    Last Post: 2nd April 2010, 06:42
  5. Replies: 1
    Last Post: 16th March 2010, 15:46

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
  •  
Qt is a trademark of The Qt Company.