微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > ARM技术讨论 > QT自动链接信号和槽的机制

QT自动链接信号和槽的机制

时间:10-02 整理:3721RD 点击:

Qt编程用自己格式写的槽一般要做槽链接,而通过ui界面转到槽则不需要。查看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 OKbutton:

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


我面试时  被问到 为什么connect  可以关联信号与槽机制

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top