1:動態渲染數據+動態添加控件(按鈕,進度條)
class MainWindow(QWidget, Ui_MainFrom): def __init__(self): super(MainWindow, self).__init__() self.setupUi(self) # #去掉標題頭 # self.setWindowFlags(Qt.FramelessWindowHint) # self.m_flag = False self.Button = QPushButton(self) self.Button.setGeometry(QtCore.QRect(880, 80, 93, 41)) # 列表顯示 self.tableWidget.setColumnCount(5) # self.tableWidget.setRowCount(3) self.tableWidget.setHorizontalHeaderLabels(('視頻網站','視頻標題','播放地址','下載進度','操作',)) self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers) # 禁止編輯 # 取數據庫數據 輸出來是個列表 sortInfo = [('1', 1, 1,),('2', 1, 1, ),('3', 1, 1,)] # 動態渲染數據 for row, row_data in enumerate(sortInfo): self.tableWidget.insertRow(row) # 插入行 for column in range(len(row_data)+2): # 需要多插入2列 # 如果遍歷數小於需要插入的函數,就顯示空 if column < len(row_data): self.tableWidget.setItem(row, column, QtWidgets.QTableWidgetItem(str(row_data[column]))) # 如果遍歷數等於需要插入的函數 if column == len(row_data): # 傳入當前id print("row_data[0]",row+1) # 添加兩列 進度條 按鈕 self.tableWidget.setCellWidget(row, column, self.ProgressBar(str(row_data[0]))) self.tableWidget.setCellWidget(row, column+1, self.buttonForRow(str(row_data[0]))) # 進度條 def ProgressBar(self, id): widget = QWidget() # 進度條 statusBar = QProgressBar() statusBar.setStyleSheet(''' text-align : center; background-color : LightCoral; height : 30px; ''') hLayout = QHBoxLayout() hLayout.addWidget(statusBar) widget.setLayout(hLayout) return widget # 列表內添加按鈕 def buttonForRow(self,id): widget = QWidget() # 路徑 downloadPath = QPushButton('路徑') downloadPath.setStyleSheet(''' text-align : center; background-color : NavajoWhite; height : 30px; border-style: outset; font : 13px ''') # 槽函數 downloadPath.clicked.connect(lambda:downloadPath_action.downloadPath((id))) # 下載 downLoad = QPushButton('下載') downLoad.setStyleSheet(''' text-align : center; background-color : DarkSeaGreen; height : 30px; border-style: outset; font : 13px; ''') # 刪除 deleteBtn = QPushButton('刪除') deleteBtn.setStyleSheet(''' text-align : center; background-color : LightCoral; height : 30px; border-style: outset; font : 13px; ''') hLayout = QHBoxLayout() hLayout.addWidget(downloadPath) hLayout.addWidget(downLoad) hLayout.addWidget(deleteBtn) hLayout.setContentsMargins(5, 2, 5, 2) widget.setLayout(hLayout) return widget def main(): app = QApplication(sys.argv) ui = MainWindow() ui.show() sys.exit(app.exec_()) if __name__ == "__main__": main()