Qt中connect函数(信号与槽)初识

Qt开发信号与槽:

一、介绍

信号槽机制与Windows下消息机制类似,消息机制是基于回调函数,Qt中用信号与槽来代替函数指针,使程序更安全简洁。信号和槽机制是 Qt 的核心机制,可以让编程人员将互不相关的对象绑定在一起,实现对象之间的通信

二、具体介绍:

信号介绍:当对象改变其状态时,信号就由该对象发射 (emit) 出去,而且对象只负责发送信号,它不知道另一端是谁在接收这个信号。这样就做到了真正的信息封装,能确保对象被当作一个真正的软件组件来使用

槽介绍:用于接收信号,而且槽只是普通的对象成员函数。一个槽并不知道是否有任何信号与自己相连接。而且对象并不了解具体的通信机制

连接介绍:信号和槽通过connect建立连接

connect使用:

connect(sender, SIGNAL(signal), receiver, SLOT(slot));

sender和receiver是对象的指针,SIGNAL和SLOT后是信号和槽相应的函数

四、具体使用:

第一个界面:

Widget类头文件:

#include <QWidget>
#include "form.h"
#include <QtWidgets>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
signals:
    void send(QString str);
private slots:
    void clickButton();

private:
    Ui::Widget *ui;
    QLineEdit * input ;
    Form * form;

};

Widget类实现:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setGeometry(100,100,400,400);
    this->setWindowTitle(QObject::tr("Login"));
    //Label
    QLabel * label = new QLabel(this);
    label->setText(QObject::tr("请输入:"));
    label->setGeometry(50,150,70,25);
    QFont fontLabel;
    fontLabel.setPointSize(15);
    label->setFont(fontLabel);
    //Line
    this->input = new QLineEdit(this);

    this->input->setGeometry(120,150,200,25);
    QString inputString = this->input->text();
    //Button
    QPushButton * buttonLg = new QPushButton(this);
    buttonLg->setGeometry(150,200,80,30);
    buttonLg->setText("Submit");


    QObject::connect(buttonLg,&QPushButton::clicked,this,&Widget::clickButton);
    form = new Form();
    QObject::connect(this,&Widget::send,form,&Form::receive);

}
void Widget::clickButton(){
    QString str = this->input->text();
    emit send(str);

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

第二个界面:

Form类头文件:


#include <QWidget>
#include<QLabel>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = 0);
    ~Form();
public slots:
    void receive(QString str);

private:
    Ui::Form *ui;
    QLabel * label;
};

Form类实现:

#include "form.h"
#include "ui_form.h"
#include <QtWidgets>
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    this->setGeometry(100,100,400,400);
    this->setWindowTitle(QObject::tr("Login"));
    //Label
    label = new QLabel(this);
    label->setText("");
    label->setGeometry(50,150,170,25);
    QFont fontLabel;
    fontLabel.setPointSize(15);
    label->setFont(fontLabel);
}

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

void Form::receive(QString str)
{
    this->label->setText(tr(" 获取的是:%1").arg(str));
    this->show();
}


  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt信号机制信号函数函数的参数不一定要完全一致。但是,有一些规则需要遵循: 1. 信号函数的参数类型必须与函数的参数类型兼容。如果信号函数有多个参数,函数的参数数量可以少于信号函数的参数数量,但是必须按照相同的顺序进行匹配。 2. 如果信号函数的参数是引用类型(如const引用),则函数的参数也必须是引用类型。同样,如果信号函数的参数是指针类型,函数的参数也必须是指针类型。 3. 如果信号函数函数的参数类型不完全匹配,但可以进行隐式转换,则也可以成功地连接信号。 4. 如果信号函数函数的参数类型不兼容,并且没有合适的隐式转换,那么连接将会失败。 以下是一个示例,演示了信号函数参数不完全一致但仍然可以连接的情况: ```cpp class MyObject : public QObject { Q_OBJECT public slots: void mySlot(int value) { // 函数代码 } }; // 创建对象和连接信号 MyObject* obj = new MyObject(); QPushButton* button = new QPushButton("Click me"); connect(button, SIGNAL(clicked()), obj, SLOT(mySlot())); // 信号函数没有参数,函数有一个int参数 ``` 在上面的示例,QPushButton的`clicked()`信号没有参数,但是成功地连接到了MyObject的`mySlot(int value)`函数。这是因为Qt会自动将`clicked()`信号的触发转换为一个不带参数的调用。 总结起来,信号函数的参数不一定要完全一致,但是必须是兼容的或者可以进行隐式转换才能成功连接。 希望这个解答对您有帮助。如有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值