Testing a button click event in QtTest
-
How to test a button click event of the button created like :
Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
in widget based app. -
As far as I know
createButton
is not a standard method of Qt. In any case, what do you mean by Testing? if it's unit testing you can use https://doc.qt.io/qt-5/qttestlib-tutorial3-example.htmlQTest::mouseClick(plusButton,Qt::LeftButton);
-
This is for unit Testing :
Calculator::Calculator(QWidget *parent)
: QWidget(parent)
{
Button *plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked()));
}
This is the code snippet and I tried
QTest::mouseClick(calculator->plusButton,Qt::LeftButton);
But it shows nomember named plusButton in calculator but in the calculator pointer suggestions additiveOperatorClicked() is shown. -
@shravan_121 said in Testing a button click event in QtTest:
But it shows nomember named plusButton in calculator
Because your plusButton is a LOCAL variable inside constructor. Add it as member variable to Calculator class.
-
@jsulm Thank You for helping me resolve the problem
-
@shravan_121 This is really basic C++, I suggest you read a book.
class Calculator { public: Button *plusButton; }; ... Calculator::Calculator(QWidget *parent) : QWidget(parent) { plusButton = createButton(tr("+"), SLOT(additiveOperatorClicked())); }