connect关联
Qt5之前:
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
Qt5开始:
connect(sender, &Sender::signal, receiver, &Receiver::slot);
前者:sender和receiver是指向QObject的指针,signal和slot是不带参数的函数名。
SIGNAL()宏和SLOT()宏会把他们的参数转换成相应的字符串。
后者:(1)编译器,检查信号与槽是否存在,参数类型检查,Q_OBJECT宏是否存在(2)信号可以和普通函数、的普通成员函数、lambda函数连接(不在局限于信号和槽函数)(3)参数可以是typedef的或者使用不同的namespace specifier(4)可以允许一些自动类型的转换(即信号和槽函数类型不必完全匹配)
在设计模式关联
自动关联
#include "widget.h"
#include "ui_widget.h"
#include<QDialog>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//QDialog dialog(this);
//非模态对话框
//QDialog* dialog = new QDialog(this);
//dialog->show();
//模态对话框1
//QDialog dialog(this);
//dialog.exec();
//模态对话框2
//QDialog* dialog = new QDialog(this);
//dialog->setModal(true);
//dialog->show();
//在构造函数中,this代表当前正在被构建的对象地址
//connect关联
connect(ui->showChildButton,&QPushButton::clicked,this,&Widget::showchildDialog);
}
Widget::~Widget()
{
delete ui;
}
void Widget::showchildDialog()
{
QDialog* dialog = new QDialog(this);
dialog->show();
}
//on_发送信号的对象名_信号函数名
//void Widget::on_showChildButton_clicked()
//{
// QDialog* dialog =new QDialog(this);
// dialog->show();
//}