Спасибо, огромное, не докурил доки…
Если кому-то менее очевидно что нужно сделать - работающий код ниже.
# -*- coding: utf-8 -*-
import operator
import sys
from PyQt4 import QtCore, QtGui
from PyQt4 import QtWebKit
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Model(QtCore.QAbstractTableModel):
def __init__(self, cached_data, parent=None, *args):
QtCore.QAbstractTableModel.__init__(self, parent, *args)
self.colLabels = (u'№ 1', u'№ 2', u'№ 3')
self.cached = cached_data
def rowCount(self, parent):
return len(self.cached)
def columnCount(self, parent):
return len(self.colLabels)
def data(self, index, role):
if not index.isValid():
return QtCore.QVariant()
elif role != QtCore.Qt.DisplayRole and role != QtCore.Qt.EditRole:
return QtCore.QVariant()
row = index.row()
col = index.column()
return QtCore.QVariant(self.cached[row][col])
def headerData(self, section, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.colLabels[section])
return QtCore.QVariant()
def sort(self, Ncol, order):
self.emit(QtCore.SIGNAL("layoutAboutToBeChanged()"))
self.cached = sorted(self.cached, key=operator.itemgetter(Ncol))
if order == QtCore.Qt.DescendingOrder:
self.cached.reverse()
self.emit(QtCore.SIGNAL("layoutChanged()"))
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("cleanlooks"))
mainWindow.setObjectName(_fromUtf8("mainWindow"))
mainWindow.setToolTip(_fromUtf8(""))
mainWindow.resize(520, 520)
self.centralwidget = QtGui.QWidget(mainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.centralwidget.setGeometry(QtCore.QRect(10, 10, 500, 500))
self.tableView_income = QtGui.QTableView(self.centralwidget)
self.tableView_income.setObjectName(_fromUtf8("tableView_income"))
self.tableView_income.setGeometry(QtCore.QRect(10, 10, 500, 500))
self.tableView_income.setSortingEnabled(True)
self.model_income = Model([(1,2,3),
(4,5,6),
(7,8,9)])
self.tableView_income.setModel(self.model_income)
self.tableView_income.resizeColumnsToContents()
"""
Меняем сигнал на такой:
self.tableView_income.selectionModel().selectionChanged.connect(self.summ_calc)
либо создаем объект вида
self.sel_model = self.tableView_income.selectionModel()
и подвязываем к нему connect
self.sel_model.connect(self.summ_calc)
"""
self.tableView_income.selectionModel().selectionChanged.connect(self.summ_calc)
def summ_calc(self):
tables_models = ((self.tableView_income, self.model_income),)
answer = []
for i in tables_models:
if i[0].hasFocus():
for j in i[0].selectedIndexes():
answer.append(float(i[1].data(j, 0).toString()))
i[0].setToolTip(u'Сумма: %.2f' % float(sum(answer)))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_mainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())