当前位置: 首页 > news >正文

用户自定义槽函数

用户自定义槽函数

一. 自定义槽函数

  1. 只有QObject的子类才能自定义槽

  2. 定义槽的类必须在声明的最开始处使用Q_OBJECT

  3. 类中声明槽时需要使用slots关键字

  4. 槽与所处理的信号在函数签名上必须移植

  5. SIGNAL和SLOT所指定的名称中:

    可以包含参数类型,不能包含具体的参数名

二. 计算器页面的按钮点击后可以打印按钮上的字符串

  1. 头文件

    #ifndef _QCALCULATORUI_H_
    #define _QCALCULATORUI_H_
    
    #include <QWidget>
    #include <QLineEdit>
    #include <QPushButton>
    
    class QCalculatorUI : public QWidget
    {
    	//定义槽的类必须再生命的最开始处使用Q_OBJECT
        Q_OBJECT
    private:
        QLineEdit* m_edit;
        QPushButton* m_buttons[20];
    
        QCalculatorUI();
        bool construct();
    private slots:
    	
        void onButtonClicked();
    public:
        static QCalculatorUI* NewInstance();
        void show();
        ~QCalculatorUI();
    };
    
    #endif
    
    
  2. 源文件

    #include "CalculatorUI.h"
    #include "ui_CalculatorUI.h"
    
    #include <QDebug>
    
    
    QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
    {
    
    }
    
    bool QCalculatorUI::construct()
    {
        bool ret = true;
        const char* btnText[20] =
        {
            "7", "8", "9", "+", "(",
            "4", "5", "6", "-", ")",
            "1", "2", "3", "*", "<-",
            "0", ".", "=", "/", "C",
        };
    
        m_edit = new QLineEdit(this);
    
        if( m_edit != NULL )
        {
            m_edit->move(10, 10);
            m_edit->resize(240, 30);
            m_edit->setReadOnly(true);
        }
        else
        {
            ret = false;
        }
    
        for(int i=0; (i<4) && ret; i++)
        {
            for(int j=0; (j<5) && ret; j++)
            {
                m_buttons[i*5 + j] = new QPushButton(this);
    
                if( m_buttons[i*5 + j] != NULL )
                {
                    m_buttons[i*5 + j]->resize(40, 40);
                    m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                    m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
    
                    //调用connect函数,将按钮的点击消息映射到某个消息处理函数
                    //注意:只有QObject的子类才能定义槽
                    //当前的点击映射到当前类对象的消息定义函数
                    connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked()));
                }
                else
                {
                    ret = false;
                }
            }
        }
    
        return ret;
    }
    
    QCalculatorUI* QCalculatorUI::NewInstance()
    {
        QCalculatorUI* ret = new QCalculatorUI();
    
        if( (ret == NULL) || !ret->construct() )
        {
            delete ret;
            ret = NULL;
        }
    
        return ret;
    }
    
    void QCalculatorUI::show()
    {
        QWidget::show();
    
        setFixedSize(width(), height());
    }
    
    //定义消息处理函数
    void QCalculatorUI::onButtonClicked()
    {
        //调用sender() 是QObject的成员函数,返回一个指针,指向信号的发送者
        QPushButton* btn = (QPushButton*)sender();
    
        qDebug() << "onButtonClicked()";
        qDebug() << btn->text();
    }
    
    QCalculatorUI::~QCalculatorUI()
    {
    
    }
    
    
    #include "CalculatorUI.h"
    
    #include <QApplication>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QCalculatorUI* cal = QCalculatorUI::NewInstance();
        int ret = -1;
    
        if( cal != NULL )
        {
            cal->show();
    
            ret = a.exec();
    
            delete cal;
        }
    
        return ret;
    }
    
    
  3. 效果
    请添加图片描述

相关文章:

  • Qt中的字符串类
  • 计算器字符串转换问题
  • QDialog - 对话框
  • 登录对话框的设计和实现
  • Qt中的标准对话框
  • 布局管理器
  • Qt工程添加资源文件(例如:图标)
  • 主窗口 (QMainWindow)
  • C++中的explicit
  • Qt中的事件处理
  • 使用二阶构造实现计算器页面的构造过程
  • Linux内核的CodingStyle
  • 常见面试题及面试准备阶段要思考的问题
  • Qt中事件的传递过程
  • Qt事件初探-发现事件
  • avalon2.2的VM生成过程
  • Java IO学习笔记一
  • Java方法详解
  • QQ浏览器x5内核的兼容性问题
  • Vim 折腾记
  • 创建一种深思熟虑的文化
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 计算机在识别图像时“看到”了什么?
  • 前端面试之闭包
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 线上 python http server profile 实践
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 译米田引理
  • 用mpvue开发微信小程序
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​io --- 处理流的核心工具​
  • #QT项目实战(天气预报)
  • #免费 苹果M系芯片Macbook电脑MacOS使用Bash脚本写入(读写)NTFS硬盘教程
  • %@ page import=%的用法
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (python)数据结构---字典
  • (蓝桥杯每日一题)love
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (十六)Flask之蓝图
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (转)Linq学习笔记
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .bat批处理(六):替换字符串中匹配的子串
  • .NET Compact Framework 3.5 支持 WCF 的子集
  • .net FrameWork简介,数组,枚举
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .NET的数据绑定
  • .NET多线程执行函数