![](/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.