ZBLOG

class ButtonItem : public QWidget { public: QPushButton *left_btn; QPushButton *right_btn; ButtonItem(int item_type, QWidget *parent = nullptr) : QWidget(parent) { if(ZIWANG == item_type || ZIWANG_HEAD == item_type) ...

要为 left_btnright_btn 设置点击后触发的槽函数,你可以使用 Qt 的信号和槽机制。以下是一个修改后的示例,演示了如何连接按钮的点击信号到相应的槽函数。

首先,你需要在 ButtonItem 类中添加两个槽函数,然后在构造函数中将按钮的点击信号连接到这些槽函数。

#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMessageBox> // 用于显示消息框(仅用于示例)

class ButtonItem : public QWidget {
    Q_OBJECT // 确保包含这个宏以支持Qt的信号与槽机制

public:
    QPushButton *left_btn;
    QPushButton *right_btn;

    ButtonItem(int item_type, QWidget *parent = nullptr) : QWidget(parent) {
        if(ZIWANG == item_type || ZIWANG_HEAD == item_type)
            return;

        QHBoxLayout *layout = new QHBoxLayout(this);
        layout->addStretch();
        
        // 左侧按钮
        left_btn = new QPushButton(this);
        left_btn->setFixedSize(24, 24);

        // 右侧按钮
        right_btn = new QPushButton(this);
        right_btn->setFixedSize(24, 24);

        if(SHEBEI_HEAD == item_type) {
            left_btn->setStyleSheet("border:none; \
                                background-image: url(:/png/yincang.png); \
                                background-repeat: no-repeat; \
                                background-position: center;");
            right_btn->setStyleSheet("border:none; \
                                background-image: url(:/png/duoxuan.png); \
                                background-repeat: no-repeat; \
                                background-position: center;");
        } else if(SHEBEI_LEVEL_1 == item_type) {
            left_btn->hide();
            right_btn->setStyleSheet("border:none; \
                               background-image: url(:/png/xiala.png); \
                               background-repeat: no-repeat; \
                               background-position: center;");
        } else if (SHEBEI_LEVEL_2 == item_type) {
            left_btn->setStyleSheet("border:none; \
                                background-image: url(:/png/yincang.png); \
                                background-repeat: no-repeat; \
                                background-position: center;");
            right_btn->setStyleSheet("border:none; \
                                background-image: url(:/png/lianjie.png); \
                                background-repeat: no-repeat; \
                                background-position: center;");
        }
        
        layout->addWidget(left_btn);
        layout->addWidget(right_btn);
        
        // 设置布局
        layout->setContentsMargins(0, 0, 0, 0);

        // 连接信号和槽
        connect(left_btn, &QPushButton::clicked, this, &ButtonItem::onLeftButtonClicked);
        connect(right_btn, &QPushButton::clicked, this, &ButtonItem::onRightButtonClicked);
    }

private slots:
    void onLeftButtonClicked() {
         QMessageBox::information(this, "Info", "Left button clicked!");
         // 在这里处理左侧按钮被点击的逻辑
    }

    void onRightButtonClicked() {
         QMessageBox::information(this, "Info", "Right button clicked!");
         // 在这里处理右侧按钮被点击的逻辑
    }
};

注意事项:

  1. 确保包含头文件:你需要在文件开头包含 <QObject> 和相关头文件,以支持信号与槽。
  2. 添加 Q_OBJECT:这个宏是必需的,确保类可以使用 Qt 的元对象系统。
  3. 编译时注意:如果你修改了类定义并加入了 Q_OBJECT 宏,需要重新运行 qmake 并进行完整编译。
  4. 实现槽功能:在上面的示例中,我使用了 QMessageBox 来演示按钮被点击后的反馈。根据你的需求,可以在对应的槽函数内实现具体的功能逻辑。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=19978

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?