Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用QPlainTextEdit编辑器结束编辑会话?_Python_Pyqt5_Edit_Qtableview - Fatal编程技术网

Python 如何使用QPlainTextEdit编辑器结束编辑会话?

Python 如何使用QPlainTextEdit编辑器结束编辑会话?,python,pyqt5,edit,qtableview,Python,Pyqt5,Edit,Qtableview,这是一个MRE: from PyQt5.QtCore import QRect, Qt, QAbstractTableModel from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QWidget, QVBoxLayout, QStyledItemDelegate, QPlainTextEdit, QShortcut import sys, types from PyQt5.QtGui import QFont

这是一个MRE:

from PyQt5.QtCore import QRect, Qt, QAbstractTableModel
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QWidget, QVBoxLayout, QStyledItemDelegate, QPlainTextEdit, QShortcut
import sys, types
from PyQt5.QtGui import QFont

class HistoryTableViewDelegate( QStyledItemDelegate ):
    def __init__( self, history_table_view ):
        super().__init__( history_table_view )
    
    def createEditor(self, parent, option, index):
        editor = QPlainTextEdit( parent )
        editor.setSizeAdjustPolicy( QPlainTextEdit.SizeAdjustPolicy.AdjustToContents )
        self.model = index.model()
        column = index.column()
        row = index.row()
        parent.parent().verticalHeader().resizeSection( row, 150 )
        parent_font = QFont( self.parent().font() )
        parent_font.setPointSize( self.parent().font().pointSize() )
        editor.setFont( parent_font )
        def end_edit():
            print( f'end edit...')
            self.setModelData( editor, self.model, index )
            # how to end the edit session programmatically at this point?
            # self.destroyEditor( editor, index ) 
            
        end_edit_shortcut = QShortcut(  'Alt+E', editor, context = Qt.ShortcutContext.WidgetShortcut )
        end_edit_shortcut.activated.connect( end_edit )
        return editor
    
    def setEditorData(self, editor, index ):
        # NB superclass method sets the editor's text to empty string...
        self.original_text = index.model().data( index, Qt.DisplayRole )
        editor.insertPlainText( str( self.original_text ) )     
        
class HistoryTableModel( QAbstractTableModel ):
    def __init__( self ):
        super(HistoryTableModel, self).__init__()
        data = [
              [4, 9, 2],
              [1, 0, 0],
              [3, 5, 0],
        ]
        self._data = data

    def data(self, index, role):
        if role == Qt.DisplayRole:
            return self._data[index.row()][index.column()]

    def rowCount(self, index):
        return len(self._data)

    def columnCount(self, index):
        return len(self._data[0])
    
    def flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
    
    def setData(self, index, value, role ):
        if role == Qt.EditRole:
            self._data[ index.row() ][ index.column() ] = value  
        return True  

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(600, 700 )
        self.centralwidget = QWidget(MainWindow)
        self.verticalLayoutWidget = QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry( QRect(20, 20, 500, 500))
        self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
        self.comps = []
        self.table_view = QTableView(self.verticalLayoutWidget)
        self.comps.append( self.table_view )
        self.table_view.setGeometry(QRect(20, 20, 200, 200))
        self.verticalLayout.addWidget(self.table_view)
        self.table_view.setModel( HistoryTableModel() )
        self.table_view.setTabKeyNavigation(False)
        self.table_view.setItemDelegate( HistoryTableViewDelegate( self.table_view ))
        MainWindow.setCentralWidget(self.centralwidget)

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

app = QApplication(sys.argv)
application = MainWindow()
application.show()
sys.exit(app.exec())
我想要一个多行编辑器,所以我想我已经明白了要做的是
QPlainTextEdit
。尽管在上述情况下,按Alt-E将数据提交到模型,但实际上并不会结束会话。例如,如果用鼠标单击另一个单元格,则可以看到编辑的多行数据仍保留在编辑的单元格中,这样做也会结束编辑会话

如何以编程方式结束会话

注意:按Alt-E后按Escape是一种方法(两次击键)。。。但我基本上想用
QLineEdit
编辑器做任何按Enter键的编程操作。

明白了

def end_edit():
    self.setModelData( editor, self.model, index )
    self.closeEditor.emit( editor )