# coding=utf-8 from PyQt4.QtGui import * from PyQt4.QtCore import * import locale import sys class MyDialog(QDialog): def __init__(self, parent=None): super(MyDialog, self).__init__(parent) self.MyTable = MyTabView(4, 3) print type(self.MyTable) self.MyTable.setHorizontalHeaderLabels(['姓名', '身高', '體重']) s = "松鼠" print s newItem = QTableWidgetItem(s) self.MyTable.setItem(0, 0, newItem) newItem = QTableWidgetItem("10cm") self.MyTable.setItem(0, 1, newItem) newItem = QTableWidgetItem("60g") self.MyTable.setItem(0, 2, newItem) layout = QHBoxLayout() layout.addWidget(self.MyTable) self.setLayout(layout) self.MyTable.setSelectionBehavior(QAbstractItemView.SelectRows) self.MyTable.setEditTriggers(QAbstractItemView.NoEditTriggers) # self.MyTable.setEditTriggers(QAbstractItemView.DoubleClicked) # self.MyTable.SelectedClicked(self.a()) def mouseDoubleClickEvent(self, event): QTableView.mouseDoubleClickEvent(self, event) pos = event.pos() item = self.indexAt(pos) print 'jianpan' if item: print "item clicked at ", item.row(), " ", item.column() class MyTabView(QTableView): def __init__(self, row,column,parent=None): super(MyTabView, self).__init__(parent) self.model = QStandardItemModel(row, column) self.setModel(self.model) def mouseDoubleClickEvent(self, event): QTableView.mouseDoubleClickEvent(self, event) pos = event.pos() item = self.indexAt(pos) if item: print "item clicked at ", item.row(), " ", item.column() if __name__ == '__main__': reload(sys) sys.setdefaultencoding('utf8') mycode = locale.getpreferredencoding() mycode = 'utf-8' code = QTextCodec.codecForName(mycode) QTextCodec.setCodecForLocale(code) QTextCodec.setCodecForTr(code) QTextCodec.setCodecForCStrings(code) app = QApplication(sys.argv) myWindow = MyDialog() myWindow.show() sys.exit(app.exec_())