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

时间:2017-10-10 07:51:18

标签: python pyqt pyqt4 python-3.6 qtablewidget

我最近开始使用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

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

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 个答案:

答案 0 :(得分:1)

您需要在代理中实施setEditorDatasetModelData,以便能够存储和检索模型的数据。对于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)