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

Double Clicking Row on PyQt4 QTableWidget sets the content to -1

我最近开始使用 PyQt4 制作 GUI 应用程序。我试图制作一个使用 QTableWidget 的应用程序。它是 2 列 table,其中第一列设置为 QLineEdit,第二列使用委托设置为 QListWidget(抱歉,如果 "set to" 不是正确的术语”)。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

除了一件事,一切似乎都很好。每当我双击 QListWidget 列中的任何单元格时,该特定单元格的内容都会设置为 -1。我没有处理 "doubleClicked" 信号。即使我这样做了(我让它在 textBrowser 上打印单元格的内容),双击单元格也将内容设置为 -1。谁能帮我解决这个问题?我曾尝试阅读 QTableWidget 文档,但没有找到任何成功。有人可以指出我错过了什么吗? 这是我的表单 init 函数 class

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)

编辑: 添加按钮的处理程序,用于将文件添加到 table

的第二列
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

谢谢

您需要在委托中实现 setEditorData and setModelData,以便能够存储和检索模型的数据。对于 list-widget 列,默认行为是显示当前索引,对于空列表将是 -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)