雙擊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