QT中信号和槽不需要调用connect而自动链接

 

在看Implayer的源代码时,发现其代码很简单,但我无论是在.ui文件还是.cpp文件中也没看到信号和槽的链接,但当单击按钮时却可以进入槽函数。implaer.cpp有很多的槽函数。

 

在网上查了下,发现这跟槽的命名有关系。

 

QT的文档是这么说的:

 

A Dialog With Auto-Connect

Although it is easy to implement a custom slot in the dialog and connect it in the constructor, we could instead use QMetaObject's auto-connection facilities to connect the OK button's clicked() signal to a slot in our subclass. uic automatically generates code in the dialog's setupUi() function to do this, so we only need to declare and implement a slot with a name that follows a standard convention:

void on_<widget name>_<signal name>(<signal parameters>);

Using this convention, we can define and implement a slot that responds to mouse clicks on the OK button:

class ImageDialog : public QDialog, private Ui::ImageDialog
{
Q_OBJECT

public:
ImageDialog(QWidget *parent = 0);

private slots:
void on_okButton_clicked();
};

Automatic connection of signals and slots provides both a standard naming convention and an explicit interface for widget designers to work to. By providing source code that implements a given interface, user interface designers can check that their designs actually work without having to write code themselves.

 

就是说如果槽的命名是这样的话:void on_<widget name>_<signal name>(<signal parameters>);

就会自动将widget name中的信号signal name和这个槽void on_<widget name>_<signal name>(<signal parameters>)链接起来。

 

这是QT不需要connect语句就可以自动链接信号和槽的机制!

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值