![](/img/trans.png)
[英]How filter rows on the basis of columns having searched text in QLineEdit and update it in QTableView
[英]How to get text from a QLineEdit to update cells in a QTableView
概述:我有一个显示QTableView
的 UI。 当用户单击表格中的一行时,该行中的数据会填充多个QLineEdit
输入字段,对应于该单元格中的信息 - 例如,“ADDRESS”列将具有相应的“ADDRESS” QLineEdit
字段,其中该地址数据将填充。
现有功能:单击一行后,用户可以单击QLineEdit
并更改列出的文本 - 例如,如果列出了错误的地址,用户可以单击“ADDRESS” QLineEdit
字段并将其更改为不同的内容。
所需的功能:我希望能够单击“保存”按钮并将数据保存在QLineEdit
,然后反映在QTableView
。
问题:单击“保存”按钮时运行的函数尝试更新QTableView
数据框并刷新视图,但似乎没有进行任何更改,并且QTableView
数据本身没有反映任何更改。
代码示例:
**注意 - 当用户点击QTableView
,会运行一个函数来初始化self.user_selection
变量,它是一个QModelIndex
对象,在下面引用。 QLineEdit
字段包含在QGridLayout
,因此使用下面的itemAtPosition
函数。 self.comp_list
是正在填充的 QTableView 对象
当用户单击“保存”时,以下函数将运行...
def update_selected_comp_entry(self):
# This will get all of the values for each column, for the row selected - this returns a
# QWidgetItem, which is why widget().text() must be used to retrieve the cell's data
items = [self.comp_details_layout.itemAtPosition(i, 1) for i in range(self.comp_details_layout.count()) if isinstance(comp_details_layout.itemAtPosition(i, 1), PyQt5.QtWidgets.QWidgetItem)]
for i, each in enumerate(items):
self.comp_list.model().setData(self.user_selection, each.widget().text())
填充QTableView
类的简化版本:
class DataFrameModel(PyQt5.QtCore.QAbstractTableModel):
def __init__(self, df=pandas.DataFrame(), parent=None):
super(DataFrameModel, self).__init__(parent)
self._dataframe = df.replace(numpy.nan, '', regex=True)
def setDataFrame(self, dataframe):
self.beginResetModel()
self._dataframe = dataframe.copy()
self.endResetModel()
# @PyQt5.QtCore.pyqtSlot() - I've tried using this decorator and it didn't do anything
# This function is my attempt at grabbing the user's input and updating
# the QTableView displayed data
def setData(self, index, value, role=PyQt5.QtCore.Qt.EditRole):
if index.isValid():
row = index.row()
col = index.column()
# I've tried with and without the line below
self.layoutAboutToBeChanged.emit()
# I've tried using set_value() as well as iloc and neither worked
self._dataframe.loc[row, col] = value
# I';ve tried with and without this line, neither worked
self.setDataFrame(self._dataframe)
# I've also tried the dataChanged signal and that didn't work either
self.layoutChanged.emit()
return True
return False
甚至不要理会setData
函数,根据您在那里拥有的内容,不需要它。 由于您已经从items
变量的单元格中获取了所有数据,因此只需使用它来更新您首先填充QTableView
的源。 由于您知道该方法有效,因此它将获取更新的数据,您可以像往常一样刷新表。
为了这个例子,让我们假设你的列标题与你的 widget().objectName() 将是你的items
变量中的每个 QWidgetItem 的相同。 您显然可以将其更改为您想要的任何内容。
您可以使用列名作为键,然后将QLineEdit
文本作为值来制作字典。
new_input = {metric.widget().objectName: metric.widget().text() for metric in items}
然后只需将该数据发送回您的数据帧。
for key, value in zip(new_input.keys(), new_input.values()):
# You said the self.user_selection was a QModelIndex, so you can get your selected
# row from there
df.loc[self.user_selection.row(), key] = value
然后只需将该数据帧发送到您的班级,就像您通常填充该表一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.