No signal is emitted in testing for signals and slots
-
test.cpp:
QTest::keyClick(ccalculator->plusButton,Qt::Key_Plus);
QSignalSpy spy(ccalculator,&Calculator::addbuttonClicked);
QTest::keyClick(ccalculator->plusButton,Qt::Key_Plus);
QCOMPARE(spy.count(),1);
the result shows the actual value is 0 and expected value is 1.calculator.cpp:
plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()this));
connect(plusButton,SIGNAL(addbuttonClicked()),this,SLOT(additiveOperatorClicked()));
additiveOperatorClicked()
{
//some codes
emit addbuttonClicked(tr("+"));
}
can anyone help me to solve this problem. -
make sure all the connections were successful. My recommendation is to use Qt5 connection syntax but if you stick with Qt4 version then wrap the connect statement in a
Q_ASSUME()
(so it asserts if the connection fails)Q_ASSUME(connect(plusButton,SIGNAL(addbuttonClicked()),this,SLOT(additiveOperatorClicked())));
-
It shows me error ::Qobject::connect:no such signal Button::additivieoperatorClickedsignal()
-
@shravan_121
pls post your whole connect statement -
calculator.h:
class Calculator : public QWidget
{
Q_OBJECTpublic:
Button *plusButton;
Button *minusButton;
Button *timesButton;
Button *divisionButton;
Calculator(QWidget *parent = 0);
public slots:
void digitClicked();
void additiveOperatorClicked();
void multiplicativeOperatorClicked();
signals:
void addbuttonClicked(const QString &data);
void multiplybuttonClicked(const QString &data);
void digitclickedsignal(const QString &data);calculator.cpp:
Calculator::Calculator(QWidget *parent)
: QWidget(parent)
{
plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()this));connect(plusButton,SIGNAL(addbuttonClicked()),this,SLOT(additiveOperatorClicked()));
void Calculator::additiveOperatorClicked()
{
Button *clickedButton = qobject_cast<Button *>(sender());
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();
if (!pendingMultiplicativeOperator.isEmpty()) {
//! [12] //! [13]
if (!calculate(operand, pendingMultiplicativeOperator)) {
abortOperation();
return;
}
display->setText(QString::number(factorSoFar));
operand = factorSoFar;
factorSoFar = 0.0;
pendingMultiplicativeOperator.clear();} if (!pendingAdditiveOperator.isEmpty()) { if (!calculate(operand, pendingAdditiveOperator)) { abortOperation(); return; } display->setText(QString::number(sumSoFar)); } else { sumSoFar = operand; } pendingAdditiveOperator = clickedOperator; waitingForOperand = true; emit addbuttonClicked(tr("+"));
-
@shravan_121 On first view you have bad syntax. Look on signal emit
addbuttonClicked(tr("+"));
which has one argument QString. In connect function you use signal without argument ```
connect(plusButton,SIGNAL(addbuttonClicked()),this,SLOT(additiveOperatorClicked()));.
Try to replace signal on
void addbuttonClicked()
or change function slot with one argument ievoid additiveOperatorClicked(QString)
and use this in connect
connect(plusButton,SIGNAL(addbuttonClicked(QString)),this,SLOT(additiveOperatorClicked(QString)));
-
But after that i am getting same error: no such signal additiveoperatorclicked(QString)
no such slot addbuttonclicked(QString) -
@shravan_121 said in No signal is emitted in testing for signals and slots:
no such signal Button::additivieoperatorClicked
And this is the problem
additivieoperatorClicked
is a signal ofCalculator
not ofButton
. You are trying to connect with a receiver being a pointer toButton
-
This post is deleted!
-
I don't know the actual solution can you please suggest a possible solution for the error.
-
@shravan_121 said in No signal is emitted in testing for signals and slots:
connect(plusButton,SIGNAL(addbuttonClicked()),
The problem is here.
addbuttonClicked
is a signal ofCalculator
butplusButton
is of typeButton
.
So either the sender (the first argument) or the signal (the second) must be changed to make sense