0

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()
Keks
  • 77
  • 7

1 Answers1

2

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.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Hi, it worked, thanks. Now I have another problem but I think it must be easy to fix. I have just edited the post. Could you help me? – Keks Apr 01 '14 at 16:18
  • @Keks Generally, if you have another problem, you should post another question. Adding problems to a correctly answered question is essentially freeloading. – Kuba hasn't forgotten Monica Apr 02 '14 at 02:16