1 Attachment(s)
There are no connection with a Button
Hello!
If I click on Button nothing is occur.
Attachment 8972
I created the project in NetBeans with this tutorial: https://netbeans.org/kb/docs/cnd/qt-applications.html
Summa.h
Code:
/*
* File: Summa.h
* Author: Ivan
*
* Created on April 19, 2013, 6:28 AM
*/
#ifndef SUMMA_H
#define SUMMA_H
class Summa {
public:
Summa(){m_summa = 0;}
double calcSumma(double a, double b){return a+b;}
double getSumma() {return m_summa;}
private:
double m_summa;
};
#endif /* SUMMA_H */
SummaDialog.h
Code:
/*
* File: SummaDialog.h
* Author: Ivan
*
* Created on April 19, 2013, 6:03 AM
*/
#ifndef _SUMMADIALOG_H
#define _SUMMADIALOG_H
#include "ui_SummaDialog.h"
class SummaDialog
: public QDialog { Q_OBJECT
public:
SummaDialog();
virtual ~SummaDialog();
public slots:
void showResult(const double& result);
private:
Ui::SummaDialog widget;
};
#endif /* _SUMMADIALOG_H */
SummaDialog.cpp
Code:
/*
* File: SummaDialog.cpp
* Author: Ivan
*
* Created on April 19, 2013, 6:03 AM
*/
#include "SummaDialog.h"
SummaDialog::SummaDialog() {
widget.setupUi(this);
connect(widget.pushButton, SIGNAL(clicked(const double&)),
this, SLOT(showResult(const double&)));
}
SummaDialog::~SummaDialog() {
}
void SummaDialog::showResult(const double& result) {
widget.
resultEdit->setText
(QString::number(result
));
}
main.cpp
Code:
/*
* File: main.cpp
* Author: Ivan
*
* Created on April 19, 2013, 6:02 AM
*/
#include <QtGui/QApplication>
#include "SummaDialog.h"
#include "Summa.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
SummaDialog sd;
sd.show();
return app.exec();
}
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?
Re: There are no connection with a Button
What I must to write?
OutPut
Quote:
Process is started in an external terminal ...
It does not work:
Code:
SummaDialog::SummaDialog() {
widget.setupUi(this);
connect(widget.pushButton, SIGNAL(clicked()),
this, SLOT(showResult(const double&)));
}
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?
Code:
connect(widget.pushButton, SIGNAL(clicked()),
this, SLOT(showResult(const double&)));
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).
Re: There are no connection with a Button
amleto, thank you very much!
Code:
/*
* File: SummaDialog.cpp
* Author: Ivan
*
* Created on April 19, 2013, 6:03 AM
*/
#include "SummaDialog.h"
#include "Summa.h"
SummaDialog::SummaDialog() {
widget.setupUi(this);
connect(widget.pushButton, SIGNAL(clicked()),
this, SLOT(calcResultSLOT()));
}
void SummaDialog::calcResultSLOT() {
// Take input data
double a = widget.aDSpinBox->value();
double b = widget.bDSpinBox->value();
// Calc the sum
Summa s;
double result = s.calcSumma(a, b);
// Show the result
widget.
resultEdit->setText
(QString::number(result
));
}
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.
Code:
void SummaDialog::calcResultSLOT() {
// Take input data
double a = widget.aDSpinBox->value();
double b = widget.bDSpinBox->value();
// Calc the sum
Summa s;
double result = s.calcSumma(a, b);
// Show the result
widget.
resultEdit->setText
(QString::number(result
));
emit resultSIGNAL(result); // add signal to your header as well -- void resultSignal(double result);
}