您的位置:首页 > 文旅 > 旅游 > shop++的发展历程_怎么开网店找货源_小红书怎么推广_杭州网站seo推广软件

shop++的发展历程_怎么开网店找货源_小红书怎么推广_杭州网站seo推广软件

2025/4/27 12:37:20 来源:https://blog.csdn.net/qq_62917398/article/details/147460910  浏览:    关键词:shop++的发展历程_怎么开网店找货源_小红书怎么推广_杭州网站seo推广软件
shop++的发展历程_怎么开网店找货源_小红书怎么推广_杭州网站seo推广软件

两种方法的对比

  • 方法1:sender()

优点:代码简洁,无需额外参数
缺点:依赖运行时类型转换,安全性较低
适用场景:简单场景,少量按钮

  • 方法2:Lambda (推荐)

优点:安全直观,直接传递标识
缺点:需要为每个按钮单独连接
适用场景:现代Qt项目,推荐首选(QT5以上才支持)

注意事项:

  • 线程安全
    • 若在多线程环境中使用 sender(),需确保发送者对象未被销毁。
  • 性能优化
    • 对高频点击按钮,避免在槽函数中执行耗时操作。
  • 代码维护
    • 使用枚举代替字符串标识(如 enum ButtonType { Prev, Next, … })可提高代码健壮性。

示例代码:

// widget.h

// widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void onButtonClicked();void handleButton(const QString &btnType); // 统一处理函数private:Ui::Widget *ui;
};
#endif // WIDGET_H

// widget.cpp

// widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);// 方法1:使用 sender() 函数识别信号源// 方法1.步骤1:统一连接按钮的 clicked 信号到槽connect(ui->btn1, &QPushButton::clicked, this, &Widget::onButtonClicked);connect(ui->btn2, &QPushButton::clicked, this, &Widget::onButtonClicked);connect(ui->btn3, &QPushButton::clicked, this, &Widget::onButtonClicked);// 方法2:Lambda 表达式传递按钮标识// 方法2:步骤1:连接信号时通过Lambda捕获按钮对象connect(ui->btn1, &QPushButton::clicked, [this]() { handleButton("prev"); });connect(ui->btn2, &QPushButton::clicked, [this]() { handleButton("next"); });connect(ui->btn3, &QPushButton::clicked, [this]() { handleButton("forecast"); });
}Widget::~Widget()
{delete ui;
}// 方法1:步骤2:在槽函数中通过 sender() 判断来源
void Widget::onButtonClicked()
{
#if 1QString objName = sender()->objectName();if (objName == "btn1") {qDebug() << "方法1-btn1被点击";} else if (objName == "btn2") {qDebug() << "方法1-btn2被点击";} else if (objName == "btn3") {qDebug() << "方法1-btn3被点击";}
#elseQPushButton *clickedButton = qobject_cast<QPushButton*>(sender());if (!clickedButton)return;// 根据按钮的 objectName 或指针地址区分if (clickedButton == ui->btn1) {qDebug() << "方法1-btn1被点击";} else if (clickedButton == ui->btn2) {qDebug() << "方法1-btn2被点击";} else if (clickedButton->objectName() == "btn3") {qDebug() << "方法1-btn3被点击";}
#endif
}
// 方法2:步骤2:统一处理函数
void Widget::handleButton(const QString &btnType)
{if (btnType == "prev") {qDebug() << "方法2-btn1被点击";} else if (btnType == "next") {qDebug() << "方法2-btn2被点击";} else if (btnType == "forecast") {qDebug() << "方法2-btn3被点击";}
}

现象:

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com