from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * import sys # font: 25 9pt "Microsoft YaHei"; class select_list(QObject): style_sheet =""" QListView { font: 25 9pt; border: 15px solid white; border-radius: 10px; show-decoration-selected: 1; } QListView::item { height: 40px; } QListView::item:hover { background-color: transparent; padding: 10px; border-left: 3px solid rgb(130, 130, 130); } QListView::item:selected { background-color: transparent; color: black; padding: 10px; border-left: 3px solid black; } """ def __init__(self,father:QDialog,title:str,str_list:list): QObject.__init__(self) self.w=QDialog(father) self.w.resize(800,400) self.w.setWindowTitle(title) self.w.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) self.w.setWindowModality(Qt.WindowModality.ApplicationModal) self.file_list = QListWidget(self.w) self.file_list.setObjectName(u"str_list") self.file_list.setGeometry(QRect(20, 20, 760, 360)) self.file_list.setFrameShape(QFrame.Shape.Box) self.file_list.setMidLineWidth(1) self.file_list.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded) self.file_list.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection) self.file_list.itemDoubleClicked.connect(self.item_clicked) self.file_list.setStyleSheet(self.style_sheet) self.item_append(str_list) def item_append(self,items:list): for i in items: # print("add item",i[0]) self.file_list.addItem(i[0]) def item_clicked(self,item:QListWidgetItem ): self.select_item=item.text() self.w.done(QDialog.DialogCode.Accepted) self.w.close() def show(self): # self.w.show() if(self.w.exec()==QDialog.DialogCode.Accepted): # print(self.select_item) return self.select_item return "" class file_list(QDialog): def __init__(self,father:QDialog,title:str,str_list:list): QDialog.__init__(self,father) self.resize(1000,400) self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) self.setWindowTitle(title) self.code_list=QTableWidget(self) self.code_list.setObjectName(u"code_list") self.code_list.setGeometry(QRect(5, 5, 990, 390)) self.code_list.setColumnCount(4) self.code_list.setHorizontalHeaderLabels(list(["ID","上传时间","文件名","描述"])) self.code_list.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) self.code_list.horizontalHeader().setSectionResizeMode(0,QHeaderView.ResizeMode.Fixed) self.code_list.setColumnWidth(0,50) self.code_list.horizontalHeader().setSectionResizeMode(1,QHeaderView.ResizeMode.Fixed) self.code_list.setColumnWidth(1,200) self.code_list.horizontalHeader().setSectionResizeMode(2,QHeaderView.ResizeMode.Fixed) self.code_list.setColumnWidth(2,200) self.code_list.setSelectionBehavior( QAbstractItemView.SelectionBehavior.SelectRows) self.code_list.cellDoubleClicked.connect(self.cell_clicked) self.backcolor=True for i in str_list: self.list_add(i[0]) # 添加数据 def list_add(self,text:str): stat=self.backcolor self.backcolor=not self.backcolor if(stat==True): bkcolor=QColor() bkcolor.setRgb(230,230,230,255) else: bkcolor=QColor() bkcolor.setRgb(255,255,255,255) code_strs=text.split("|") if(len(code_strs)>=4): code_strs[3],line=self.split_line(code_strs[3]) else: code_strs.append("") line=1 RowCont=self.code_list.rowCount() self.code_list.insertRow(RowCont) for i in range(len(code_strs)): self.code_list.setItem(RowCont,i,QTableWidgetItem(code_strs[i])) self.code_list.item(RowCont,i).setBackground(bkcolor) self.code_list.setRowHeight(RowCont,line*30) # self.code_list.resizeRowsToContents() def split_line(self,text:str): s='' line=(len(text)+29)//30 for i in range(line): s+=text[i*30:(i+1)*30] s+='\n' return s.strip(),line def cell_clicked(self,x:int,y:int ): print("cell_clicked",x,y) str_list=[] for i in range(3): str_list.append(self.code_list.item(x,i).text()) self.select_item='|'.join(str_list) self.done(QDialog.DialogCode.Accepted) self.close() def show(self): if(self.exec()==QDialog.DialogCode.Accepted): print(self.select_item) return self.select_item return "" if __name__=='__main__': app = QApplication(sys.argv) l=[ '1|2023-11-01 11:11:00|dddddddd|测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字,测试文字, 测试文字,测试文字,测试文字,', '2|sssssss|dddddddd|sssssssss', '3|sssssss|dddddddd|sssssssss', '4|sssssss|dddddddd|sssssssss', ] dlg=file_list(father=None, title="测试窗口",str_list=l) dlg.show() # app.exec()