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

推荐订阅源

I
Intezer
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
H
Heimdal Security Blog
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
Apple Machine Learning Research
Apple Machine Learning Research
Spread Privacy
Spread Privacy
腾讯CDC
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
Google Developers Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
J
Java Code Geeks
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
Google Online Security Blog
Google Online Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
W
WeLiveSecurity
宝玉的分享
宝玉的分享
aimingoo的专栏
aimingoo的专栏
博客园_首页
S
Security @ Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Privacy International News Feed
H
Hacker News: Front Page
Vercel News
Vercel News
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
Schneier on Security
Schneier on Security
Last Week in AI
Last Week in AI
H
Help Net Security
M
MIT News - Artificial intelligence
美团技术团队

博客园 - 大强跑了

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