簡體   English   中英

如何從 QLineEdit 獲取文本以更新 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM