QTableView无法将预期的FocusIn / FocusOut事件发送到eventFilter

时间:2017-01-25 13:00:45

标签: python pyqt qtablewidget qtablewidgetitem qevent

我有一个QTableWidget,其中包含需要大量水平空间的浮点数或复杂条目。通过字符串格式化显示减少位数的值工作正常,但显然我在表中编辑和存储条目时会失去精度。

我通过使用eventFilter找到了QLineEdit小部件的解决方案:FocusIn事件将存储的值以完全精度复制到QLineEdit文本字段,FocusOut个事件或Return_Key个商店更改的值并用减少的位数覆盖文本字段。

使用与QTableWidgets相同的方法会给出以下(可能相关)问题:

  • FocusIn和FocusOut事件未按预期生成:当我双击某个项目时,我收到一个FocusOut事件,点击另一个项目会产生一个FocusIn事件
  • 我无法复制已编辑的所选项目的内容,我总是获得未经编辑的价值。
  • 点击选择项目不会产生任何事件。

我已经尝试过评估QTableWidgetItem事件,但我没有收到任何事件 - 我是否需要在每个QTableWidgetItem上设置事件过滤器?如果是这样,每次调整表格大小时(我的应用程序中经常这样做),是否需要断开QTableWidgetItem eventFilters?用QLineEdit小部件填充我的表是否有意义?

附加的MWE并不是很小,但我可以进一步缩小它。

# -*- coding: utf-8 -*-
#from PyQt5.QWidgets import ( ...)
from PyQt4.QtGui import (QApplication, QWidget, QTableWidget, QTableWidgetItem, 
                         QLabel, QVBoxLayout)
import PyQt4.QtCore as QtCore
from PyQt4.QtCore import QEvent
from numpy.random import randn

class EventTable (QWidget):
    def __init__(self, parent = None):
        super(EventTable, self).__init__(parent)
        self.myTable = QTableWidget(self)
        self.myTable.installEventFilter(self) # route all events to self.eventFilter()

        myQVBoxLayout = QVBoxLayout()
        myQVBoxLayout.addWidget(self.myTable)
        self.setLayout(myQVBoxLayout)

        self.rows = 3; self.columns = 4 # table + data dimensions
        self.data = randn(self.rows, self.columns) # initial data
        self._update_table() # create table 

    def eventFilter(self, source, event):
        if isinstance(source, (QTableWidget, QTableWidgetItem)):
#            print(type(source).__name__, event.type()) #too much noise
            if event.type() == QEvent.FocusIn:  # 8: enter widget
                print(type(source).__name__, "focus in")
                self.item_edited = False
                self._update_table_item() # focus: display data with full precision
                return True # event processing stops here

            elif event.type() == QEvent.KeyPress:
                print(type(source).__name__, "key pressed")
                self.item_edited = True # table item has been changed
                key = event.key() # key press: 6, key release: 7
                if key in {QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter}: # store entry
                    self._store_item() # store edited data in self.data
                    return True
                elif key == QtCore.Qt.Key_Escape: # revert changes
                    self.item_edited = False
                    self._update_table() # update table from self.data
                    return True

            elif event.type() == QEvent.FocusOut: # 9: leave widget
                print(type(source).__name__, "focus out")
                self._store_item() 
                self._update_table_item() # no focus: use reduced precision
                return True

        return super(EventTable, self).eventFilter(source, event)

    def _update_table(self):
        """(Re-)Create the table with rounded data from self.data """
        self.myTable.setRowCount(self.rows)
        self.myTable.setColumnCount(self.columns)
        for col in range(self.columns):
            for row in range(self.rows):
                self.myTable.setItem(row,col, 
                        QTableWidgetItem(str("{:.3g}".format(self.data[row][col]))))
        self.myTable.resizeColumnsToContents()
        self.myTable.resizeRowsToContents()

    def _update_table_item(self, source = None):
        """ Re-)Create the current table item with full or reduced precision. """
        row = self.myTable.currentRow()
        col = self.myTable.currentColumn()
        item = self.myTable.item(row, col)
        if item: # is something selected?
            if not item.isSelected(): # no focus, show self.data[row][col] with red. precision
                print("\n_update_item (not selected):", row, col)
                item.setText(str("{:.3g}".format(self.data[row][col])))
            else: #  in focus, show self.data[row][col] with full precision
                item.setText(str(self.data[row][col]))
                print("\n_update_item (selected):", row, col)

    def _store_item(self):
        """ Store the content of item in self.data """
        if self.item_edited:
            row = self.myTable.currentRow()
            col = self.myTable.currentColumn()
            item_txt = self.myTable.item(row, col).text()
            self.data[row][col] = float(str(item_txt))
            print("\n_store_entry - current item/data:", item_txt, self.data[row][col])



if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    mainw = EventTable()
    app.setActiveWindow(mainw) 
    mainw.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

你以完全错误的方式解决这个问题。现有的API已经满足了这些用例的需求,因此有几种解决方案比您现有的解决方案简单得多。

最简单的可能是使用QStyledItemDelegate并重新实现其dispalyText方法。这将允许您将完整值存储在表中,但以不同方式对其进行格式化以便显示。编辑时,将始终显示完整值(作为字符串):

from PyQt4.QtGui import (QApplication, QWidget, QTableWidget, QTableWidgetItem,
                         QLabel, QVBoxLayout,QStyledItemDelegate)
import PyQt4.QtCore as QtCore
from PyQt4.QtCore import QEvent
from numpy.random import randn

class ItemDelegate(QStyledItemDelegate):
    def displayText(self, text, locale):
        return "{:.3g}".format(float(text))

class EventTable (QWidget):
    def __init__(self, parent = None):
        super(EventTable, self).__init__(parent)
        self.myTable = QTableWidget(self)
        self.myTable.setItemDelegate(ItemDelegate(self))
        myQVBoxLayout = QVBoxLayout()
        myQVBoxLayout.addWidget(self.myTable)
        self.setLayout(myQVBoxLayout)
        self.rows = 3; self.columns = 4 # table + data dimensions
        self.data = randn(self.rows, self.columns) # initial data
        self._update_table() # create table

    def _update_table(self):
        self.myTable.setRowCount(self.rows)
        self.myTable.setColumnCount(self.columns)
        for col in range(self.columns):
            for row in range(self.rows):
                item = QTableWidgetItem(str(self.data[row][col]))
                self.myTable.setItem(row, col, item)
        self.myTable.resizeColumnsToContents()
        self.myTable.resizeRowsToContents()

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    mainw = EventTable()
    app.setActiveWindow(mainw)
    mainw.show()
    sys.exit(app.exec_())

注意:使用item roles来解决此问题很诱人。但是,QTableWidgetItemQStandardItem都将DisplayRoleEditRole视为一个角色,这意味着有必要重新实现其data和{{1}获取所需功能的方法。