深入探究connect函数

来源:互联网 发布:php gzip 解压缩 编辑:程序博客网 时间:2024/06/05 21:59
首先我们都知道QObject的connect函数的作用是用来连接信号和槽的。
我们再来具体看看connect函数:
connect(sender, SIGNAL(signal), receiver, SLOT(slot));
sender和receiver是对象指针,signal和slot是转换信号和槽。
请看下面代码的具体实现:
//datherdialog.h
#ifndef FATHERDIALOG_H
#define FATHERDIALOG_H
#include <QDialog>
namespace Ui {
class FatherDialog;
}
class FatherDialog : public QDialog
{
    Q_OBJECT
public:
    explicit FatherDialog(QWidget *parent = 0);
    ~FatherDialog();
private slots:
    void on_pushButton_clicked();
    void showChildDialog();
private:
    Ui::FatherDialog *ui;
};
#endif // FATHERDIALOG_H
//fatherdialog.cpp
#include "fatherdialog.h"
#include "ui_fatherdialog.h"
#include <QMessageBox>
FatherDialog::FatherDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FatherDialog)
{
    ui->setupUi(this);
    QObject::connect(ui->childB,  //这里把“点击按钮”这个信号与“展示子窗口”这个槽连接在一起,即点击按钮后立即执行showChildDialog函数
                     &QPushButton::clicked,
                     this,
                     &FatherDialog::showChildDialog);
}
FatherDialog::~FatherDialog()
{
    delete ui;
}
void FatherDialog::on_pushButton_clicked()
{
    QMessageBox::information(this,"提示","<font size='26'>请告诉我为什么</font>",QMessageBox::Ok);
    //这里是pushButton按钮点击后响应的函数,同样把信号和槽连接在了一起,
    //这里根据在界面操作的不同有可能用到了connect函数,也可能是connectSlotsByName函数
}
void FatherDialog::showChildDialog()
{
    QDialog * d= new QDialog(this);
    d->show();
}
原创粉丝点击