将 MouseDoubleClickEvent 添加到 PyQt5 中的 for 循环中制作的小部件 [重复]

Posted

技术标签:

【中文标题】将 MouseDoubleClickEvent 添加到 PyQt5 中的 for 循环中制作的小部件 [重复]【英文标题】:Add MouseDoubleClickEvent to widgets made in a for loop in PyQt5 [duplicate] 【发布时间】:2021-05-15 07:32:53 【问题描述】:

我想将 PyQt5 中的 MouseEvent 添加到在 for 循环中创建的每个 QLabel 中,并在双击标签时更改标签颜色。

我的代码

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtCore
import sys

App = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(100, 100, 540, 800)
win.setStyleSheet("background-color: white;")

_1to90numberslabel = []


def numberEventHandler(number, event=None):
    print(number)
    print(event)
    print(event.type())
    print(event.x(), event.y())


numberBorder = QLabel(win)

numberBorder.resize(330, 380)
numberBorder.setStyleSheet("border: 3px solid black;"
                           "border-radius: 10px;"
                           "background-color: #7de3ff")
# bordercoordx, bordercoordy = int(numbers_x+10), int(numbers_y+20)
bordercoordx, bordercoordy = int((win.width() / 2) - (numberBorder.width() / 2)), 250
numberBorder.move(bordercoordx, bordercoordy)

numbers_x = -10
numbers_y = -20

numberFontBold = QFont('Arial', 15)
numberFontBold.setBold(True)

numberFontNotBold = QFont('Arial', 15)
numberFontNotBold.setBold(False)

num = 1
for i in range(9):
    # print(i, end="\n\n\n")

    for j in range(10):
        # print(j)
        label = QLabel(numberBorder)
        label.move(int((30 * (j + 1)) + numbers_x), int((40 * (i + 1)) + numbers_y))
        # print(int((30 * (j+1)) + numbers_x), int((40 * (i+1)) + numbers_y))
        label.setStyleSheet("background-color: #7de3ff;color: #2200ff;border: 0px solid "
                            "black;border-radius:5px;z-index:2") 
        label.setAlignment(QtCore.Qt.AlignCenter)
        label.setText(f"num")
        label.setFont(numberFontNotBold)
        label.mouseDoubleClickEvent = lambda e: numberEventHandler(number=num, event=e)
        label.adjustSize()

        num += 1
        _1to90numberslabel.append(label)

win.show()
sys.exit(App.exec())

【问题讨论】:

【参考方案1】:

一种方法是继承QLabel 并覆盖doubleClickEvent

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtCore
import sys


# custom class for label
class MyLabel(QLabel):
    # define style sheets for base state and double clicked state of the label
    BASE_STYLESHEET = "background-color: #7de3ff;color: #2200ff;border: 0px solid black;border-radius:5px;z-index:2"
    CLICKED_STYLESHEET = "background-color: #ffff00;color: #2200ff;border: 0px solid black;border-radius:5px;z-index:2"

    def __init__(self, num, parent=None):
        super().__init__(parent)
        self.setStyleSheet(MyLabel.BASE_STYLESHEET)
        self.setAlignment(QtCore.Qt.AlignCenter)
        self.setText(f"num")
        self.setFont(numberFontNotBold)
        self.adjustSize()

    def mouseDoubleClickEvent(self, event):
        self.setStyleSheet(MyLabel.CLICKED_STYLESHEET)


# This part is as before minus the numberEventHandler function
App = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(100, 100, 540, 800)
win.setStyleSheet("background-color: white;")

_1to90numberslabel = []

numberBorder = QLabel(win)

numberBorder.resize(330, 380)
numberBorder.setStyleSheet("border: 3px solid black;"
                           "border-radius: 10px;"
                           "background-color: #7de3ff")
# bordercoordx, bordercoordy = int(numbers_x+10), int(numbers_y+20)
bordercoordx, bordercoordy = int((win.width() / 2) - (numberBorder.width() / 2)), 250
numberBorder.move(bordercoordx, bordercoordy)

numbers_x = -10
numbers_y = -20

numberFontBold = QFont('Arial', 15)
numberFontBold.setBold(True)

numberFontNotBold = QFont('Arial', 15)
numberFontNotBold.setBold(False)

num = 1
for i in range(9):
    for j in range(10):
        # use MyLabel for the labels instead of standard QLabel. Note that the setup of the label is taken care of
        # in the __init__ method of the custom class
        label = MyLabel(num, numberBorder)
        label.move(int((30 * (j + 1)) + numbers_x), int((40 * (i + 1)) + numbers_y))
        num += 1
        _1to90numberslabel.append(label)

win.show()
sys.exit(App.exec())

【讨论】:

以上是关于将 MouseDoubleClickEvent 添加到 PyQt5 中的 for 循环中制作的小部件 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式将 selectableItemBackground 添​​加到 ImageButton?

QT学习小结之鼠标事件

通过 Cloudformation 模板将 Lambda 函数添​​加到 Kinesis Firehose

将 lambda 函数添​​加到类的符号表中

2018新品系列之atom66,静电家族添一员“小”将

添加多项选择分数