#!/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)