您的位置:首页 > 编程语言 > Qt开发

QT中代码中与设计器中控件信号与SLOT连接(原来还可以这样连接)

2016-06-29 05:29 731 查看






双击testqt.ui



托一个push button到窗体中,双击,可以输入字符



按F4或 menu->edit->edit signals/slots 定义SLOT





选择已定义好的SLOT,点确定就可以进行关联了。

定义自定义SLOT:

点上面对话框中的EDIT按钮,弹出:





注意这里自定义的slot必须是

on_<object name>_<signal name>(<signal parameters>)

格式。



然后再在testqt.h头文件中加入下面声明:

public slots:

void on_testQt_clicked ();

在testqt.cpp中加入函数实现:

void testQt::on_testQt_clicked ()

{

QMessageBox msg;

msg.setText("ok");

msg.exec(); //模式对话框,show显示非模式对话框

}

编译后,你可以在ui_testqt.h头文件中看到

QObject::connect(pushButton,SIGNAL(clicked()),testQtClass,SLOT(on_testQt_clicked ()));

QMetaObject::connectSlotsByName(testQtClass);

例如UI里新建了一个openButton,在.h文件里声明void on_openButton_clicked()函数并在cpp文件里添加这个函数的定义后,seupUi()就可以自动将openButton的clicked信号与我们定义的slot函数联系在一起了!

我们的.ui文件自动生成的ui_mainwindow.h文件里的代码总会有一句:

QMetaObject::connectSlotsByName(MainWindowClass);

它就是用来自动识别我们所有界面控件的信号槽的,但必须是以下面的格式。

void QMetaObject::connectSlotsByName ( QObject * object ) [static]

Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:

public slots:

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

Let's assume our object has a child object of type QPushButton with the object name button1. The slot to catch the button's clicked() signal would be:

void on_button1_clicked();
http://blog.csdn.net/kl222/article/details/7739141
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: