[PySide] Disconnect the "currentChanged" signal on a qtableview widget

1,202 views
Skip to first unread message

david hautbois

unread,
Nov 12, 2011, 3:37:16 PM11/12/11
to pys...@lists.pyside.org
Hi
I connected a signal on a qtableview widget :
self.tableView.connect(self.tableView.selectionModel(),
QtCore.SIGNAL("currentChanged(const QModelIndex &, const QModelIndex
&)"), self.selectionChanged)

I tried many ways to disconnect it :
self.tableView.disconnect( QtCore.SIGNAL("currentChanged(const
QModelIndex &, const QModelIndex &)"), self.selectionChanged)
self.tableView.disconnect( QtCore.SIGNAL("currentChanged"),
self.selectionChanged)
self.tableView.disconnect( QtCore.SIGNAL("currentChanged"))
self.tableView.disconnect( "currentChanged")
...
...

Nothing works....

Thanks.

David.
(A pyside newbie....)
_______________________________________________
PySide mailing list
PyS...@lists.pyside.org
http://lists.pyside.org/listinfo/pyside

david hautbois

unread,
Nov 12, 2011, 3:42:56 PM11/12/11
to pys...@lists.pyside.org
I find the good way :
self.tableView.disconnect(self.tableView.selectionModel(),
QtCore.SIGNAL("currentChanged"), self.selectionChanged)

Aaron Richiger

unread,
Nov 13, 2011, 7:07:46 AM11/13/11
to pys...@lists.pyside.org
Hello!

I wrote a little snippet to demonstrate a tableview with the slot you
wanted and how to disconnect it. Please note, that this code is reduced
to the minimum and therefor will not win the Miss Code 2011 award... But
I hope, it shows you the possibility. To show, that the slot works, I
first connect for 5 seconds (so select an item quickly) and then
disconnect it after using a timer. You used "the old" signal/slot
syntax. I used the new one, I think this is more pythonic and readable:
view.selectionModel().currentChanged.disconnect(slot)

Hope it helps!
Aaron

from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtSql import *
import sys

class TableModel(QAbstractTableModel):
def __init__(self, addresses=None, parent=None):
super(TableModel, self).__init__(parent)
self.table = [[1, 2, 3], [4, 5, 6]]

def rowCount(self, index=QModelIndex()):
return len(self.table)

def columnCount(self, index=QModelIndex()):
return len(self.table[0])

def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self.table[index.row()] [index.column()]
else:
return None

def setData(self, index, value, role=Qt.EditRole):
self.table[index.row()] [index.column()] = value
return True

def flags(self, index):
return Qt.ItemIsEditable | Qt.ItemIsEnabled

def slotCurrentChanged(index1, index2):
print 'Index 1:', index1.row(), index1.column()
print 'Index 2:', index2.row(), index2.column()
print

def slotDisconnect():
view.selectionModel().currentChanged.disconnect(slotCurrentChanged)


app = QApplication(sys.argv)
model = TableModel()
view = QTableView()
view.setModel(model)
view.selectionModel().currentChanged.connect(slotCurrentChanged)
QTimer.singleShot(5000, slotDisconnect)
view.show()
sys.exit(app.exec_())

Reply all
Reply to author
Forward
0 new messages