pyqt5 自动生成label,单独信号绑定

14 篇文章 0 订阅

请添加图片描述

#!/usr/bin/ python
# -*- encoding: utf-8 -*-
'''
@文件    :auto.py
@说明    : 自动生成label 案例
@时间    :2022/04/19 17:27:28
@作者    :刘子沫
@邮箱    :spiritai@qq.com
@版本    :1.0
'''


import time
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(675, 299)
        self.scrollArea = QtWidgets.QScrollArea(Dialog)
        self.scrollArea.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.scrollArea.setGeometry(QtCore.QRect(10, 10, 666, 279))
        self.scrollArea.setWidgetResizable(False)
        self.scrollArea.setObjectName("scrollArea")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "自动生成label"))
        
class MyLabel(QLabel):
    def __int__(self):
        super(MyLabel, self).__init__()

class mwindow(QWidget, Ui_Dialog):
    def __init__(self):
        super(mwindow, self).__init__()
        self.setupUi(self)

    def createlabel(self):
        w,h = 120,130
        num = 20
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, w*num, 261))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        label = []
        for i in range(num):
            label.append(MyLabel(self.scrollAreaWidgetContents))
            label[-1].setGeometry(QtCore.QRect(w*i,0, w, h))
            label[-1].setObjectName("label_"+str(i))
            label[-1].setText("label_"+str(i))
            label[-1].setStyleSheet("background-color:rgb(255,255,255);")

        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        
if __name__ == '__main__':
    app=QApplication(sys.argv)
    #初始化窗口
    m=mwindow()
    m.show()
    m.createlabel()
    sys.exit(app.exec_())


监测内部点击信号,当前label对象被点击时


# 添加点击信号
class myLabel(QLabel):
    clicked = pyqtSignal(int,str,str,str,str,str,str,bytes)
    # 鼠标点击事件
    def mousePressEvent(self, QMouseEvent):
        if QMouseEvent.button() == Qt.LeftButton:
            self.clicked.emit(self.imgId,self.imgName,self.time,self.place,self.author,self.imgType,self.imgDesc,self.imgbyte)


'''
显示详情页面
'''
def show_detail(self,imgId,imgName,time,place,author,imgType,imgDesc,imgbyte):
	pass


# 创建label对象并绑定点击事件
myLabel().clicked.connect(show_detail)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图像处理大大大大大牛啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值