Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

PYQT4_PGSQL_PSYCOPG2

By: a guest on Sep 25th, 2012  |  syntax: Python  |  size: 1.73 KB  |  hits: 64  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import psycopg2
  4. import sys
  5. from PyQt4 import QtCore, QtGui
  6.  
  7. con = None
  8.  
  9. class MainWindow(QtGui.QWidget):
  10.     updateSignal = QtCore.pyqtSignal()
  11.     def __init__(self, parent=None):
  12.         super(MainWindow, self).__init__(parent)
  13.         self.table_widget = QtGui.QTableWidget()
  14.         self.button = QtGui.QPushButton('Populate')
  15.         self.button.clicked.connect(self.populate)
  16.         layout = QtGui.QVBoxLayout()
  17.         layout.addWidget(self.table_widget)
  18.         layout.addWidget(self.button)
  19.         self.setLayout(layout)
  20.         self.updateSignal.connect(self.update_table)
  21.         self.populate()
  22.  
  23.     def populate(self):
  24.         con = psycopg2.connect(database='testdb', user='Arthur')
  25.         cur = con.cursor()
  26.         cur.execute('SELECT title,fname FROM customer')
  27.         data = cur.fetchall()
  28.        
  29.         linhas = len(data)
  30.         colunas =  len(data[0])
  31.         i = 1
  32.         j = 0
  33.         self.table_widget.setSortingEnabled(False)
  34.         self.table_widget.setRowCount(linhas)
  35.         self.table_widget.setColumnCount(colunas)
  36.         self.table_widget.setHorizontalHeaderLabels(['Titulo', 'Nome'])
  37.             for i in range(linhas):#percorrer as linhas
  38.                for j in range(colunas):#percorrer as colunas
  39.                    print  j
  40.                    item = QtGui.QTableWidgetItem(data[i][j])
  41.                    self.table_widget.setItem(i, j, item)
  42.                    self.updateSignal.emit()
  43.                    self.table_widget.setSortingEnabled(True)
  44.  
  45.     def update_table(self):
  46.         self.table_widget.sortItems(0,QtCore.Qt.DescendingOrder)
  47.  
  48.  
  49. if __name__ == "__main__":
  50.     app = QtGui.QApplication(sys.argv)
  51.     wnd = MainWindow()
  52.     wnd.resize(640, 480)
  53.     wnd.show()
  54.     sys.exit(app.exec_())