如何使用 QPlainTextEdit 编辑器结束编辑会话?
How to end an edit session with QPlainTextEdit editor?
这是一个 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 将数据提交给模型,但它实际上并没有结束会话。例如,如果您用鼠标单击另一个单元格,您可以看到编辑后的多行数据保留在编辑后的单元格中,这样做也会以某种方式结束编辑会话。
如何以编程方式结束会话?
NB 在按 Alt-E 后按 Escape 是一种方法(两次击键)...但我基本上想用 QLineEdit
编辑器按 Enter 做任何编程的事情。
知道了...
def end_edit():
self.setModelData( editor, self.model, index )
self.closeEditor.emit( editor )
这是一个 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 将数据提交给模型,但它实际上并没有结束会话。例如,如果您用鼠标单击另一个单元格,您可以看到编辑后的多行数据保留在编辑后的单元格中,这样做也会以某种方式结束编辑会话。
如何以编程方式结束会话?
NB 在按 Alt-E 后按 Escape 是一种方法(两次击键)...但我基本上想用 QLineEdit
编辑器按 Enter 做任何编程的事情。
知道了...
def end_edit():
self.setModelData( editor, self.model, index )
self.closeEditor.emit( editor )