如何让QLineEdit和QPushButton在PyQt5的表格视图中以这样的列和样式显示?

问题描述 投票:0回答:1

我有3张三张图片,如下所示:

picture1

enter image description here

enter image description here

如何让QLineEdit和QPushButton在PyQt5的表格视图中以这样的列和样式显示?

我有下面显示的以下三张图片,

我想编写一个通过PyQt5实现这些功能的GUI:

  1. 单击鼠标一次时,将选择此行,并突出显示此行1。就像数字1指向
  2. 几秒钟后,在“单击此处添加文件”处再次单击鼠标一次,它将进入编辑模式。就像数字2指向的一样,第二行中将显示QLineEdit和QPushButton'...'。如果单击“ ...”,然后弹出“文件选择对话框”,则当我选择文件时,它将按文件绝对路径替换“单击此处添加文件”。

    注意:不要双击鼠标进入编辑模式,应先单击鼠标一次,几秒钟后再次单击鼠标,将进入编辑模式。 当我选择一个绝对路径非常长的文件时。我可以在QPushButton'...'后面看到一些字符显示, 看起来像QLineEdit右边的QPushButton重叠。

  3. 完成第2步后,如果继续在其他行中单击鼠标,则第2步中的QLineEdit和QPushButton'...'将消失,就像'VAR(“ myModelConer”)行]

我有很多天研究3个特征,但无法获得我的期望风格。作为示例,我将在这里给出我的代码,它是2行2列。任何人都可以帮助我修改和完成上述3个功能。

提前感谢

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Delegate(QStyledItemDelegate):
    def __init__(self, parent=None):
        super(Delegate, self).__init__(parent)

    def createEditor(self, parent, option, index):
        if index.column() == 0:
            lineedit    = QLineEdit("$woroot/1.scs",parent)
            pushbutton  = QPushButton("...", parent)
            #lineedit   = QLineEdit("..",self.parent())
            #pushbutton = QPushButton("...", self.parent())
            lineedit.index       = [index.row(), index.column()]
            pushbutton.index    = [index.row(), index.column()]
            h_box_layout = QHBoxLayout()
            h_box_layout.addWidget(lineedit)
            h_box_layout.addWidget(pushbutton)
            h_box_layout.setContentsMargins(0, 0, 0, 0)
            h_box_layout.setAlignment(Qt.AlignCenter)
            widget = QWidget()
            widget.setLayout(h_box_layout)
            self.parent().setIndexWidget(
                index,
                widget
            )
        elif index.column() == 1:
            combobox = QComboBox(parent)
            combobox.addItems(section_list)
            combobox.setEditable(True)
            #combobox.editTextChanged.connect(self.commitAndCloseEditor)        
            return combobox

    def setEditorData(self, editor, index):
        text = index.model().data(index, Qt.DisplayRole)
        print "setEditorData, text=", text
        text = str(text)
        i = editor.findText(text)
        print "i=", i
        if i == -1:     
            i = 0
        editor.setCurrentIndex(i)  

    def setModelData(self, editor, model, index):

        text = editor.currentText()
        if len(text) >= 1:
            model.setData(index, text)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

    def commitAndCloseEditor(self):
        editor = self.sender()
        if isinstance(editor, (QTextEdit, QLineEdit,QSpinBox,QComboBox)):
            self.commitData[QWidget].emit(editor)
            self.closeEditor[QWidget].emit(editor)
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    model = QStandardItemModel(4, 2)
    tableView = QTableView()
    tableView.setModel(model)
    delegate = Delegate(tableView)
    tableView.setItemDelegate(delegate)
    section_list = ['w','c','h']
    for row in range(4):
        for column in range(2):
            index = model.index(row, column, QModelIndex())
            model.setData(index, (row + 1) * (column + 1))
    tableView.setWindowTitle("Spin Box Delegate")
    tableView.show()
    sys.exit(app.exec_())
pyqt5 qtableview qlineedit qpushbutton qstyleditemdelegate
1个回答
1
投票

如果您想为编辑器使用复杂的窗口小部件,则当然不应该在setIndexWidget()中使用createEditor,因为您将失去对它的直接访问并对其进行控制。而是返回复杂的小部件,并确保setModelData和setEditorData均正常运行。

要检查“延迟”单击,还需要覆盖editorEvent()以确保该事件实际上是鼠标左键单击。但是,这还远远不够:项目视图的选择始终会因事件循环的周期而延迟,因此单击后立即获取当前选择是不可靠的,因为它随后会更新;您需要使用单次QTimer才能正确检查表的选择和当前索引。

最后,无需检查委托中的列,只需使用editorEvent()

setItemDelegateForColumn()
© www.soinside.com 2019 - 2024. All rights reserved.