2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PyQt5】クリック可能なラベルを自作する

Posted at

作った時の環境

Python 3.9.4

PyQt5==5.15.4
PyQt5-Qt5==5.15.2
PyQt5-sip==12.8.1
sip==6.0.3

コード

from PyQt5.QtWidgets import QLabel
from PyQt5.QtCore import Qt, pyqtSignal
 
class ClickableLabel(QLabel):
    
    clicked = pyqtSignal()
    
    def __init__(self, parent=None):
        super(ClickableLabel, self).__init__(parent)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.clicked.emit()
        return QLabel.mousePressEvent(self, event)

使い方

cl = ClickableLabel()
cl.clicked.connect(スロット)

他は通常のQLabelと同じ。

現場からは以上です。

参考記事

シグナルとスロット詳細 - yu00’s blog
python - pyqtで隠しボタンのようなものを作りたい - スタック・オーバーフロー
Python 3.x - Python、Pyqt5でQLineEditで、クリックされたQlineEditのオブジェクト名をラベルに表示したい|teratail

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?