QlistWidget-具有多个信号/连接的列表?

时间:2018-10-28 18:01:18

标签: python pyqt pyqt5 qmainwindow qlistwidget

我正在为自己的任务构建简单的GUI。 是否可以通过多个信号/连接来制作QlistWidget或类似的东西? 无法找到将多个信号分配给列表中多个元素的好方法。

我需要的是类似列表(带有复选框)的东西。

  • 列表中的每个元素都应执行不同个分配的任务。
  • 我可以标记3个复选框
  • 按下按钮后-执行列表中标记元素的任务。

my gui

# -- coding: utf-8 --

from PyQt5.QtWidgets import QMainWindow,QAction,QListWidget, qApp, QGridLayout, QWidget, QDesktopWidget,QPushButton, QTextEdit,QListView,QTreeView,QListWidgetItem
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, pyqtSignal
import qdarkstyle

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow,self).__init__(parent)

        self.main_frame()
        self.center() #center frame
        self.layout_init() #widgets layout

    def main_frame(self):
        ### actions on meenubar
        exitAct = QAction(QIcon('exit.png'), '&Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(qApp.quit)
        self.statusBar()

        ### menubar
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)
        fileMenu = menubar.addMenu('&VIew')
        fileMenu = menubar.addMenu('&Help')

        ### basic geometry and color
        self.setWindowTitle('DIGITAL CARE - Data Processing - WAREX Checklist')
        self.setStyleSheet((qdarkstyle.load_stylesheet_pyqt5()))

    def layout_init(self):
        grid = QGridLayout()
        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        ### widgets
        pb = QPushButton(self.tr("Run process"))
        te = QTextEdit()
        test = QTextEdit()

        ### List withc checkboxes
        checklist = ["Pliki z Errorami","Fatal w Komponentach","Play - automat do Aktywacji","Play - maile przy Dezaktywacji",\
                     "Play - komunikaty Dezaktywacyjne","Automat do przedłużania polis",\
                     "X-KOM - ilość polis","Neonet - ilość polis","Satysfakcja - ilość polis","PLK - zaczytanie plików do bazy"]

        doubleClicked = pyqtSignal()

        ## Select all/unselect all
        model = QListWidget()
        selectall = QListWidgetItem("Select all")
        model.addItem(selectall)

        for i in checklist:
            item = QListWidgetItem(i)
            item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
            item.setCheckState(Qt.Unchecked)
            model.addItem(item)

        #selectall.itemClicked.connect(self.select_unselectall)
        #model.itemDoubleClicked.connect(self.select_unselectall)
        #model.currentItemChanged.connect(self.select_unselectall
        #model.itemDoubleClicked.connect(self.select_unselectall) doubleclick

        #for element in range(model.count()):
           # print(model.item(element).text())
            #print(model.itemActivated.connect(self.select_unselectall))
            #if model.itemActivated.emit(model.currentItem()) == selectall:

        if model.itemClicked.emit(selectall) == "Select all":
            model.itemActivated.connect(self.select_unselectall)
                #model.itemActivated.emit(self.)

        ### make vidgets alive
        self.centralWidget().setLayout(grid)
        grid.addWidget(model, 0, 0)
        grid.addWidget(te, 0, 1)
        grid.addWidget(pb, 1, 1)
        grid.addWidget(test, 1, 0)

    def select_unselectall(self, item):
        print(item.text())
        #for index in range(self.model.count()):
            #if self.model.item(index).checkState() == Qt.Checked:
               # self.model.item(index).setCheckState(Qt.Unchecked)

    def center(self):
        ###center main window
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        qr.moveCenter(cp)
        self.move(qr.topLeft())

1 个答案:

答案 0 :(得分:1)

您有两种类型的任务,当您单击全选时,您必须选择所有内容,但是当按下按钮时,任务是过滤选中的元素,在下面的示例中,我将展示如何通过{分别是clickeditemClicked的{​​1}}和QPushButton信号,您不应发射该信号,而应收听它。

QListWidget