Question

I am new to python and have a following problem with Qt; when I run this code and select an item in the table i created, my scene_selection_changed slot is not executed, or at least it seemes so. Can anyone tell me why?

    class MyWidget(QWidget):


def __init__(self, parent=None):
    super(MyWidget, self).__init__(parent)
    self.scene = QGraphicsScene()
    self.view = QGraphicsView(self.scene)
    self.view.show()
    self.parent_proxy = self.scene.addWidget(self)
    self.parent_layout = QGraphicsLinearLayout(Qt.Vertical, self.parent_proxy)

    self.table_view = QTableWidget(2,1)
    item = QTableWidgetItem("Item1")
    item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
    self.table_view.setItem(0, 0, item)
    item = QTableWidgetItem("Item2")
    item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
    self.table_view.setItem(1, 0, item)
    self.table_proxy = self.scene.addWidget(self.table_view)
    self.parent_layout.addItem(self.table_proxy)


    self.scene.selectionChanged.connect(self.scene_selection_changed)


def scene_selection_changed(self):
    print "Scene selection changed"


if __name__ == '__main__':
  app = QApplication(sys.argv)
  widget = MyWidget()

  widget.show()
  app.exec_()
  sys.exit()
Was it helpful?

Solution

Scene items can be selectable, and scene selection refers to selecting of items. You have only one scene item (a QGraphicsProxyWidget for displaying a table) and it is not selectable, so scene selection is always empty and cannot be changed.

You need to use selection tracking of the table widget itself. It refers to selected table cells.

self.table_view.itemSelectionChanged.connect(self.scene_selection_changed)

If you want to find out which cells was selected, you also should request this information from the table widget, not from the scene.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top