Python 如何从QLineEdit获取文本以更新QTableView中的单元格
概述:我有一个显示Python 如何从QLineEdit获取文本以更新QTableView中的单元格,python,pyqt5,qtableview,Python,Pyqt5,Qtableview,概述:我有一个显示QTableView的UI。当用户单击表中的一行时,该行中的数据将填充多个与该单元格中的信息相对应的QLineEdit输入字段-例如,“ADDRESS”列将有一个相应的“ADDRESS”QLineEdit字段,地址数据将在其中填充 现有功能:单击行后,用户可以单击QLineEdit并更改列出的文本-例如,如果列出了错误的地址,用户可以单击“地址”QLineEdit字段并将其更改为其他内容 所需功能:我希望能够单击“保存”按钮,将数据保存在QLineEdit中,然后反映在QTab
QTableView
的UI。当用户单击表中的一行时,该行中的数据将填充多个与该单元格中的信息相对应的QLineEdit
输入字段-例如,“ADDRESS”列将有一个相应的“ADDRESS”QLineEdit
字段,地址数据将在其中填充
现有功能:单击行后,用户可以单击QLineEdit
并更改列出的文本-例如,如果列出了错误的地址,用户可以单击“地址”QLineEdit
字段并将其更改为其他内容
所需功能:我希望能够单击“保存”按钮,将数据保存在QLineEdit
中,然后反映在QTableView
中
问题:单击“保存”按钮时运行的函数尝试更新QTableView
dataframe并刷新视图,但似乎没有进行任何更改,QTableView
数据本身没有反映任何更改
代码示例:
**注意-当用户单击QTableView
时,会运行一个函数来初始化self.user\u选择
变量,该变量是QModelIndex
对象,下面引用。
QLineEdit
字段包含在QGridLayout
中,因此可以使用下面的itemAtPosition
功能。
self.comp\u 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
然后像通常填充该表一样将该数据帧发送给类