信号与槽

继承自QObject的类中,除了c++的成员函数之外还有槽函数

槽函数可以通过代码手动调用,也可以通过信号调用

信号与槽的连接:

使用QObject的静态成员函数connect进行连接

bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection ) [static]
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. Returns true if the connection succeeds; otherwise returns false.

信号与槽的参数个数,类型顺序必须匹配

You must use the SIGNAL() and SLOT() macros when specifying the signal and the method, for example:
SIGNAL() and SLOT() 中的函数参数只写参数类型就行(如果有参数的话),不应该加形式参数

 QLabel *label = new QLabel;
 QScrollBar *scrollBar = new QScrollBar;
 QObject::connect(scrollBar, SIGNAL(valueChanged(int)),
                  label,  SLOT(setNum(int)));

自定义信号:

自定义信号时确保该类继承与QObject或其子类,在类声明中使用宏Q_OBJECT

定义信号时,只需写信号函数的声明,不需要定义,返回类型为void


class 类名:public QObject或其子类{

Q_OBJECT

//其他成员

signals:

void 信号名(参数);

};

发送信号:emit 信号函数


槽函数的定义和声明:

槽 函数不仅需要声明,而且需要定义(因为要有操作的实现)

class 类名:public QObject或其子类{

Q_OBJECT

//其他成员

public slots:    //public 或private 或protect

void 槽名(参数);

};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值