代码之家  ›  专栏  ›  技术社区  ›  0Nicholas

双击PyQt4 QTableWidget上的行将内容设置为-1

  •  1
  • 0Nicholas  · 技术社区  · 7 年前

    我最近开始使用PyQt4制作GUI应用程序。我试图制作一个应用程序,其中使用了QTableWidget。这是一个2列表,其中第一列设置为QLineEdit,第二列使用委托设置为QListWidget(抱歉,如果“设置为”不是正确的术语)。QListWidget列中的条目来自QFileDialog的列表输出

    import sys
    from PyQt4.QtGui import *
    from PyQt4 import uic
    from PyQt4.QtCore import *
    
    qtCreatorFile = "app_name.ui"  # UI File designed using QtDesigner
    Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
    
    class DelegateLEdit(QStyledItemDelegate):
        def createEditor(self, parent, option, index):
            line_edit = QLineEdit(parent)
            return line_edit
    
    class DelegateLWidget(QStyledItemDelegate):
        def createEditor(self, parent, option, index):
            list_widget = QListWidget(parent)
            list_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
            return list_widget
    

    初始化 我的表单类的功能

    def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.list_widget = QListWidget()
        self.add_button.clicked.connect(self.add_to_list)
        # self.table.itemDoubleClicked.connect(self.print_cell_content)
    
        self.table.setColumnCount(2)
    
        first_header = "Option"
        second_header = "Directories"
    
        header = first_header + ";" + second_header
        self.table.setHorizontalHeaderLabels(header.split(";"))
        header = self.table.horizontalHeader()
    
        header.setResizeMode(1, QHeaderView.ResizeToContents)
    
        self.table.resizeColumnsToContents()
        # self.table.setShowGrid(False)
    
        self.delegate_lw = DelegateLWidget(self)
        self.delegate_le = DelegateLEdit(self)
    
        self.table.setItemDelegateForColumn(0, self.delegate_le)
        self.table.setItemDelegateForColumn(1, self.delegate_lw)
    

    编辑: 添加按钮的处理程序,该按钮将文件添加到表的第二列

    def add_to_list(self):
    
        file_dialog = QFileDialog(self)
        file_dialog.saveState()
        file_dialog.setOption(QFileDialog.DontUseNativeDialog, True)
    
    
        file_list = file_dialog.getOpenFileNames()
        self.dir_list.extend(file_list)
        self.dir_list = list(set(self.dir_list)) 
        self.add_items(self.dir_list)  
    
    def add_items(self, list_items):
        list_items.sort()
        column = 1
        self.row_count = 0
        for item in list_items:
            self.table.insertRow(self.row_count)
            self.table.setItem(self.row_count, column, QTableWidgetItem(item))
            self.row_count += 1
    

    1 回复  |  直到 3 年前
        1
  •  2
  •   ekhumoro    7 年前

    setEditorData setModelData -1 对于空列表。

    以下是委托类的外观:

    class DelegateLEdit(QStyledItemDelegate):
        def createEditor(self, parent, option, index):
            line_edit = QLineEdit(parent)
            return line_edit
    
        def setEditorData(self, editor, index):
            editor.setText(index.data())
    
        def setModelData(self, editor, model, index):
            model.setData(index, editor.text())
    
    class DelegateLWidget(QStyledItemDelegate):
        def createEditor(self, parent, option, index):
            list_widget = QListWidget(parent)
            list_widget.setSelectionMode(QAbstractItemView.ExtendedSelection)
            list_widget.addItems('One Two Three Four'.split()) # or whatever
            return list_widget
    
        def setEditorData(self, editor, index):
            for line in index.data().splitlines():
                for item in editor.findItems(line, Qt.MatchExactly):
                    item.setSelected(True)
    
        def setModelData(self, editor, model, index):
            text = '\n'.join(item.text() for item in editor.selectedItems())
            model.setData(index, text)