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

长平狐 发布于 2013/01/11 10:33
阅读 1K+
收藏 0

 

在看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语句就可以自动链接信号和槽的机制!

 


原文链接:http://blog.csdn.net/fjb2080/article/details/6558589
加载中
返回顶部
顶部