信号槽中的函数重载

信号槽中的函数重载

  • QT4的方式
  • QT5的方式
    • 函数指针重载函数
    • QT5信号函数重载
      • 解决方案
  • 总结

QT4的方式

Qt4中声明槽函数必须要使用 slots 关键字, 不能省略。

信号函数:
信号槽中的函数重载_第1张图片
槽函数:
信号槽中的函数重载_第2张图片
信号槽中的函数重载_第3张图片
mainwondow:
信号槽中的函数重载_第4张图片
信号槽中的函数重载_第5张图片
cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tom = new me(this);
    teacher = new myteacher(this);
    //connect(ui->pushButtonqt4,&QPushButton::clicked,this,&MainWindow::sendMsg);
    //qt4的连接方式
    connect(ui->pushButtonqt4,SIGNAL(clicked()),this,SLOT(sendMsg()) );
    connect(tom,SIGNAL(sendMsg()),teacher,SLOT(receiveMsg()));
    connect(tom,SIGNAL(sendMsg(QString )),teacher,SLOT(receiveMsg(QString )));

    //qt5的连接方式



}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::sendMsg()
{
    qDebug()<<"调用一次";

    //tom->sendMsg();

    QString s =" hello";
    tom->sendMsg(s);
}


调用有参的信号函数:
在这里插入图片描述
调用无参的信号函数:

信号槽中的函数重载_第6张图片

在这里插入图片描述

QT5的方式

函数指针重载函数

信号槽中的函数重载_第7张图片
信号槽中的函数重载_第8张图片
信号槽中的函数重载_第9张图片

QT5信号函数重载

信号函数和槽函数和之前QT4中是一样的。
信号函数;
信号槽中的函数重载_第10张图片
槽函数:
信号槽中的函数重载_第11张图片
信号槽中的函数重载_第12张图片
关联方式:
信号槽中的函数重载_第13张图片

报错如下:E:\Code\Qt\mySignalAndSlot3\mainwindow.cpp:20: error: no matching function for call to 'MainWindow::connect(me*&, , myteacher*&, )' connect(tom,&me::sendMsg,teacher,&myteacher::receiveMsg); ^

信号和槽都是通过函数名去关联函数的地址, 但是这个同名函数对应两块不同的地址, 一个带参, 一个不带参, 因此编译器就不知道去关联哪块地址了, 所以如果我们在这种时候通过以上方式进行信号槽连接, 编译器就会报错。

解决方案

可以通过定义函数指针的方式指定出函数的具体参数,这样就可以确定函数的具体地址了。
定义函数指针指向重载的某个信号或者槽函数,在connect()函数中将函数指针名字作为实参就可以了。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tom = new me(this);
    teacher = new myteacher(this);
    //connect(ui->pushButtonqt4,&QPushButton::clicked,this,&MainWindow::sendMsg);
    //qt4的连接方式
//    connect(ui->pushButtonqt4,SIGNAL(clicked()),this,SLOT(sendMsg()) );
//    connect(tom,SIGNAL(sendMsg()),teacher,SLOT(receiveMsg()));
//    connect(tom,SIGNAL(sendMsg(QString )),teacher,SLOT(receiveMsg(QString )));

    //qt5的连接方式
    //函数指针
    void (me::*sendMsg1)() ;
    void (me::*sendMsg2)(QString s);
    void (myteacher::*receiveMsg1)();
    void (myteacher::*receiveMsg2)(QString s);
    sendMsg1 = &me::sendMsg;
    sendMsg2 = &me::sendMsg;
    receiveMsg1 = &myteacher::receiveMsg;
    receiveMsg2 = &myteacher::receiveMsg;


    connect(ui->pushButtonqt4,&QPushButton::clicked,this,&MainWindow::sendMsg);
    //connect(tom,&me::sendMsg,teacher,&myteacher::receiveMsg);
    //connect(tom,&me::sendMsg,teacher,&myteacher::receiveMsg);
    connect(tom,sendMsg1,teacher,receiveMsg1);
    connect(tom,sendMsg2,teacher,receiveMsg2);



}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::sendMsg()
{
    qDebug()<<"调用一次";

    tom->sendMsg();

    QString s =" hello";
    tom->sendMsg(s);
}


在这里插入图片描述

总结

  1. Qt4的信号槽连接方式因为使用了宏函数, 宏函数对用户传递的信号槽不会做错误检测, 容易出bug
  2. Qt5的信号槽连接方式, 传递的是信号槽函数的地址, 编译器会做错误检测, 减少了bug的产生
  3. 当信号槽函数被重载之后, Qt4的信号槽连接方式不受影响
  4. 当信号槽函数被重载之后, Qt5中需要给被重载的信号或者槽定义函数指针

你可能感兴趣的:(Qt开发,qt)