1
votes

I wrote a simple QT calculator in VS2013. I used the signal released() to call my slots, but my slot won't work. Maybe my signal never triggered. I'm new to QT, and I don't know what I did wrong.

My class has this property:

class Calculator : public QMainWindow
{
Q_OBJECT

public:
    Calculator(QWidget *parent = 0);
    ~Calculator();

private slots:
    void Calculator::two();
private:
    QLabel *lable;  
    QPushButton *two_button;
    QString value;
    QString total;
    int fnum;
    int snum;
    bool addbool;
    bool subtractbool;
    bool multiplybool;
    bool devidebool;
};

This is my line of code for connecting the signal to the slot:

one_button = new QPushButton("2", this);

connect(two_button, SIGNAL(released()), this, SLOT(two()));

and my slot is

void Calculator::two()
{
    value = value+"2";
    lable->setText(value);
}

I put a breakpoint in my slot, but it never hit the breakpoint.

1
did you mean to use clicked() instead of released()?Mike
@Mike release should also be called though I guess when you click the button.Hayt
you declared your slot wrong: not void Calculator::two(); but void two();Hayt
Typo at one_button = new QPushButton("2",this); but connect(two_button,SIGNAL(re...?Sebastian Lange

1 Answers

7
votes

You should check the result of the connect function. Your slot needs to be defined the same way you provide it to connect if you're using the old signal/slot syntax, so

// this seems to be a non-standard extension of MSVC
// doesn't even compile under gcc, clang
void Calculator::two(); 

should become

void two();

But you should use the syntax introduced with Qt 5:

connect(two_button, &QPushButton::released, this, &Calculator::two);

and it wouldn't have mattered.