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

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LINUX DO - 最新话题
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Attack and Defense Labs
Attack and Defense Labs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
小众软件
小众软件
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
T
Threatpost
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CERT Recently Published Vulnerability Notes
AI
AI
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Schneier on Security
I
Intezer
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
V2EX
aimingoo的专栏
aimingoo的专栏
量子位
NISL@THU
NISL@THU
N
News and Events Feed by Topic
O
OpenAI News
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
Schneier on Security
Schneier on Security
S
Secure Thoughts
The Cloudflare Blog
J
Java Code Geeks

博客园 - 大强跑了

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,不知道这是什么特性,感觉自由度很高,有点意思。