PDA

View Full Version : There are no connection with a Button



8Observer8
20th April 2013, 13:00
Hello!

If I click on Button nothing is occur.

8972

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

Summa.h


/*
* 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


/*
* 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


/*
* 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


/*
* 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);

QApplication app(argc, argv);

SummaDialog sd;
sd.show();

return app.exec();
}

amleto
20th April 2013, 13:05
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?

8Observer8
20th April 2013, 13:11
What I must to write?

OutPut

Process is started in an external terminal ...

It does not work:

SummaDialog::SummaDialog() {
widget.setupUi(this);
connect(widget.pushButton, SIGNAL(clicked()),
this, SLOT(showResult(const double&)));
}

amleto
20th April 2013, 13:48
I say again,

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



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

8Observer8
20th April 2013, 15:16
amleto, thank you very much!



/*
* 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));
}

amleto
20th April 2013, 16:06
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.


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