usr bin env python sqlcursorview py Copyright 2006 Torsten Marek This

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
"""
sqlcursorview.py
Copyright (C) 2006 Torsten Marek
This file is part of the examples for the Introducing PyQt4 for GUI Application
Development talk given at EuroPython 2006 at CERN, Geneva, Switzerland.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
import sys
from PyQt4 import QtCore, QtGui
from pysqlite2 import dbapi2
class DbAPICursorModel(QtCore.QAbstractItemModel):
def __init__(self):
QtCore.QAbstractItemModel.__init__(self)
self._resultRows = []
self._columnTitles = []
def columnCount(self, parent):
return len(self._columnTitles)
def rowCount(self, parent):
return len(self._resultRows)
def setResultset(self, cursor):
self._resultRows = cursor.fetchall()
self._columnTitles = [desc[0] for desc in cursor.description]
self.reset()
def index(self, row, column, parent):
return self.createIndex(row, column, 0)
def data(self, index, role):
if not (index.isValid() and role == QtCore.Qt.DisplayRole):
return QtCore.QVariant()
value = self._resultRows[index.row()][index.column()]
if value is None:
value = ""
if isinstance(value, str):
try:
value = unicode(value, "UTF-8")
except UnicodeError:
value = unicode(value, "ISO-8859-15")
else:
value = unicode(value)
return QtCore.QVariant(value)
def headerData(self, section, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self._columnTitles[section])
return QtCore.QVariant()
app = QtGui.QApplication(sys.argv)
model = DbAPICursorModel()
view = QtGui.QTableView()
sort = QtGui.QSortFilterProxyModel()
sort.setSourceModel(model)
view.setModel(sort)
view.horizontalHeader().setSortIndicatorShown(True)
view.horizontalHeader().setClickable(True)
QtCore.QObject.disconnect(view.horizontalHeader(), QtCore.SIGNAL("sectionPressed(int)"),
view, QtCore.SLOT("selectColumn(int)"))
QtCore.QObject.connect(view.horizontalHeader(), QtCore.SIGNAL("sectionClicked(int)"),
view, QtCore.SLOT("sortByColumn(int)"))
view.setAlternatingRowColors(True)
conn = dbapi2.connect(":memory:")
query = conn.cursor()
query.execute("create table person(id int primary key, "
"firstname varchar(20), lastname varchar(20))")
query.execute("insert into person values(101, 'Danny', 'Young')")
query.execute("insert into person values(102, 'Christine', 'Holand')")
query.execute("insert into person values(103, 'Lars', 'Gordon')")
query.execute("insert into person values(104, 'Roberto', 'Robitaille')")
query.execute("insert into person values(105, 'Maria', 'Papadopoulos')")
#QtCore.disconnect(view, "columnClicked(
query.execute("SELECT * FROM person")
model.setResultset(query)
view.show()
app.exec_()