代码之家  ›  专栏  ›  技术社区  ›  mike rodent

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

  •  0
  • mike rodent  · 技术社区  · 4 年前

    以下是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 编辑。

    0 回复  |  直到 4 年前
        1
  •  0
  •   mike rodent    4 年前

    知道了。。。

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