QAction에서 클립 보드의 구조화 된 텍스트를 QTableWidget에 붙여 넣으려고합니다. 이것은 내 현재 코드입니다 :QTableWidget에서 빈 셀을 편집 할 수 있는지 확인하는 방법은 무엇입니까?
나는 세포가 데이터를 붙여 넣기 전에 편집하고,이를 위해 내가QTableWidget.item(row, col)
를 사용하여 항목을 가져온 다음 내가 항목의 플래그를 확인하는 경우 테스트 할
class PasteCellsAction(qt.QAction):
def __init__(self, table):
if not isinstance(table, qt.QTableWidget):
raise ValueError('CopySelectedCellsAction must be initialised ' +
'with a QTableWidget.')
super(PasteCellsAction, self).__init__(table)
self.table = table
self.setText("Paste")
self.setShortcut(qt.QKeySequence('Ctrl+V'))
self.triggered.connect(self.pasteCellFromClipboard)
def pasteCellFromClipboard(self):
"""Paste text from cipboard into the table.
If the text contains tabulations and
newlines, they are interpreted as column and row separators.
In such a case, the text is split into multiple texts to be paste
into multiple cells.
:return: *True* in case of success, *False* if pasting data failed.
"""
selected_idx = self.table.selectedIndexes()
if len(selected_idx) != 1:
msgBox = qt.QMessageBox(parent=self.table)
msgBox.setText("A single cell must be selected to paste data")
msgBox.exec_()
return False
selected_row = selected_idx[0].row()
selected_col = selected_idx[0].column()
qapp = qt.QApplication.instance()
clipboard_text = qapp.clipboard().text()
table_data = _parseTextAsTable(clipboard_text)
protected_cells = 0
out_of_range_cells = 0
# paste table data into cells, using selected cell as origin
for row in range(len(table_data)):
for col in range(len(table_data[row])):
if selected_row + row >= self.table.rowCount() or\
selected_col + col >= self.table.columnCount():
out_of_range_cells += 1
continue
item = self.table.item(selected_row + row,
selected_col + col)
# ignore empty strings
if table_data[row][col] != "":
if not item.flags() & qt.Qt.ItemIsEditable:
protected_cells += 1
continue
item.setText(table_data[row][col])
if protected_cells or out_of_range_cells:
msgBox = qt.QMessageBox(parent=self.table)
msg = "Some data could not be inserted, "
msg += "due to out-of-range or write-protected cells."
msgBox.setText(msg)
msgBox.exec_()
return False
return True
.
내 문제는 .item
메서드가 빈 셀에 대해 None
을 반환하므로 빈 셀의 플래그를 검사 할 수 없다는 것입니다. 내 코드는 현재 붙여 넣기 영역에 빈 셀이없는 경우에만 작동합니다.
오류 라인 46 (None
반환) 및 50 (AttributeError: 'NoneType' object has no attribute 'flags'
)에 : 셀이 항목의 플래그를 확인 이외의 편집 가능한 경우 찾는 또 다른 방법은
item = self.table.item(selected_row + row,
selected_col + col)
# ignore empty strings
if table_data[row][col] != "":
if not item.flags() & qt.Qt.ItemIsEditable:
...
있습니까?
셀이 비어 있기 때문에 '없음'을 반환하지 않지만 셀이 존재하지 않기 때문에 – Chr
나는 이것을 잘 모릅니다. 내 테이블 위젯에서 빈 셀을 시각적으로 볼 수 있습니다. 셀에 데이터 또는 플래그가 설정되어 있지 않으면 항목으로 존재하지 않는다는 것을 의미합니까? – PiRK
그리고 대답이 '예'인 경우 셀의 존재하지 않음이 편집 가능합니까? 아니면 내 위젯 사용자가 항목을 만들지 않고도 쓰기 금지 기능을 사용할 수 있습니까? – PiRK