【问题标题】:emit signal if cell of QTableView is currently selected [duplicate]如果当前选择了 QTableView 的单元格,则发出信号[重复]
【发布时间】:2020-10-02 05:42:09
【问题描述】:

如果单元格被点击,我已将数据插入到QTableView 显示的单元格中 它向向该单元格插入数据的函数发出信号

 self.tableview.clicked.connect(self.insertdata_onclick)



def insertdata_onclick(self, data):
        x = self.tableview.selectionModel().currentIndex().row()
        y = self.tableview.selectionModel().currentIndex().column()
        self.datamodel.input_data[x][y] = data
        self.datamodel.layoutChanged.emit()
        # cell selected
        # get position of that cell
        # change data of that cell

如果当前选择了单元格,我如何复制此行为?

【问题讨论】:

  • self.tableview.activated.connect(self.insertdata_onclick)self.tableview.pressed.connect(self.insertdata_onclick)

标签: python pyqt pyqt5 qtableview


【解决方案1】:

点击信号带有索引而不是数据:

void QAbstractItemView::clicked(const QModelIndex &index)

所以而不是

def insertdata_onclick(self, data):

你应该像这样:

def insertdata_onclick(self, index):
    data_i_want_to_insert = self.get_my_data_somewhere(index.row(), index.column())
    self.datamodel.input_data[index.row()][index.column()] = data_i_want_to_insert 
    self.datamodel.layoutChanged.emit()

如果您单击单元格,它将被选中,除非处于“NoSelection”模式。您无需测试“当前是否选择了单元格”。

【讨论】:

  • 它只是从原始代码中复制而来。您可能希望将“data_i_want_to_insert”更新到您的表模型中。如果您的表模型的数据源是 model.input_data 作为列表列表(例如 [[1, 2], [3, 4], ] ),则该行将起作用,因为它将数据放入现场。
  • 但是,如果您想改为点击单元格中的数据,并在其他地方使用该数据,例如将其发送给您的朋友,您可以通过 index.data(some_qt_data_role) 获取数据
  • 抱歉复制了错误的代码我的意思是这一行`data_i_want_to_insert = self.get_my_data_somere(index.row(), index.column())`,如果我有 self.data = [1] 我需要为其设置索引以将其插入单元格?
  • 这一行应该找到您要插入给定(行、列)的数据。所以在你的评论中,你想为任何行和列插入 [1] 吗?如果是这样的话,data_i_want_to_insert = self.data
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2023-01-25
  • 2017-06-22
相关资源
最近更新 更多