|
3 | 3 |
|
4 | 4 | @author: Jev Kuznetsov
|
5 | 5 | '''
|
6 |
| -from PyQt4.QtCore import ( |
7 |
| - QAbstractTableModel, Qt, QVariant, QModelIndex, SIGNAL) |
8 |
| -from PyQt4.QtGui import ( |
9 |
| - QApplication, QDialog, QVBoxLayout, QTableView, QWidget) |
| 6 | +try: |
| 7 | + from PyQt4.QtCore import QAbstractTableModel, Qt, QVariant, QModelIndex |
| 8 | + from PyQt4.QtGui import ( |
| 9 | + QApplication, QDialog, QVBoxLayout, QTableView, QWidget) |
| 10 | +except ImportError: |
| 11 | + from PySide.QtCore import QAbstractTableModel, Qt, QModelIndex |
| 12 | + from PySide.QtGui import ( |
| 13 | + QApplication, QDialog, QVBoxLayout, QTableView, QWidget) |
| 14 | + QVariant = lambda value=None: value |
10 | 15 |
|
11 | 16 | from pandas import DataFrame, Index
|
12 | 17 |
|
@@ -57,9 +62,17 @@ def flags(self, index):
|
57 | 62 | return flags
|
58 | 63 |
|
59 | 64 | def setData(self, index, value, role):
|
60 |
| - self.df.set_value(self.df.index[index.row()], |
61 |
| - self.df.columns[index.column()], |
62 |
| - value.toPyObject()) |
| 65 | + row = self.df.index[index.row()] |
| 66 | + col = self.df.columns[index.column()] |
| 67 | + if hasattr(value, 'toPyObject'): |
| 68 | + # PyQt4 gets a QVariant |
| 69 | + value = value.toPyObject() |
| 70 | + else: |
| 71 | + # PySide gets an unicode |
| 72 | + dtype = self.df[col].dtype |
| 73 | + if dtype != object: |
| 74 | + value = None if value == '' else dtype.type(value) |
| 75 | + self.df.set_value(row, col, value) |
63 | 76 | return True
|
64 | 77 |
|
65 | 78 | def rowCount(self, index=QModelIndex()):
|
|
0 commit comments