简体   繁体   中英

SIGNAL SLOT in QT

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM