惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
The Cloudflare Blog
L
LangChain Blog
T
Tenable Blog
GbyAI
GbyAI
C
Cybersecurity and Infrastructure Security Agency CISA
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
量子位
Cloudbric
Cloudbric
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
S
Securelist
S
Schneier on Security
F
Full Disclosure
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy International News Feed
宝玉的分享
宝玉的分享
A
About on SuperTechFans
博客园 - 【当耐特】
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
Y
Y Combinator Blog
月光博客
月光博客
H
Hacker News: Front Page
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
N
Netflix TechBlog - Medium

博客园 - 大强跑了

MacOS应用主菜单连接到ViewController的事件 设置ios/iPad应用允许分屏 记录常用的几条gitee命令 编译安装低版本的vapor toolbox vmware fusion打不开dev vmmon断裂管道 macOS升级ruby macOS生成icns文件的脚本 OBJC调试日志控制的宏定义 开发AppleScript时查看程序UI元素的工具 mac进入恢复模式 macos升级后安装cocoapods DataGridView自定义RichTextBox列 C#winform的datagridview设置选中行 Other Linker flags 添加 -Objc导致包冲突 nat打洞原理和实现 成为顶尖自由职业者必备的七个软技能之四:如何成为销售之王(转) mac下编译FFmpeg-Android iOS app submission : missing 64-bit support eclipse显示adb is down错误,无法真机调试
QLabel加入点击的几种方式
大强跑了 · 2025-10-09 · via 博客园 - 大强跑了

QLabel默认不支持点击事件,但可以通过捕捉鼠标事件实现点击处理。

1、通过eventFilter事件过滤器

self.labelSignUp.installEventFilter(self)

def eventFilter(self, obj, event):
        """重写事件过滤器"""
        # 判断事件源是否为目标label,且事件为鼠标点击
        if obj == self.labelSignUp and event.type() == QEvent.MouseButtonPress:
            # 判断是否为左键点击
            if event.button() == Qt.LeftButton:
                print("事件过滤器:Label被左键点击了")
                return True  # 事件已处理
        return super().eventFilter(obj, event)  # 其他事件交给父类处理

 2、自定义QLabel,加入点击事件处理

class ClickableLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)

    def mousePressEvent(self, event):
        self.clicked.emit()

    clicked = QtCore.pyqtSignal()

label = ClickableLabel("点击我")
label.clicked.connect(lambda: print("QLabel被点击了"))

 如果在designer上设计的UI,需要把Label控件提升为ClickableLabel。

第二种方式发现一个有意思的地方,如果通过pyqtSlot绑定点击事件时,信号名称可以随意改的。例如:

信号定义为clicked,可以用on_label_clicked进行绑定;

信号定义为pushed,用on_label_pushed绑定。

没有系统学习过pyqt,不知道这是什么特性,感觉自由度很高,有点意思。