Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 28th, 2012  |  syntax: Python  |  size: 1.51 KB  |  views: 147  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. 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.  
  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.populate()
  21.  
  22.     def populate(self):
  23.         con = psycopg2.connect(database='testdb', user='Arthur')
  24.         cur = con.cursor()
  25.         cur.execute('SELECT title,fname FROM customer')
  26.         data = cur.fetchall()
  27.        
  28.         linhas = len(data)
  29.         colunas =  len(data[0])
  30.         i = 1
  31.         j = 0
  32.         self.table_widget.setSortingEnabled(True)
  33.         self.table_widget.setRowCount(linhas)
  34.         self.table_widget.setColumnCount(colunas)
  35.         self.table_widget.setHorizontalHeaderLabels(['Titulo', 'Nome'])
  36.         for i in range(linhas):#percorrer as linhas
  37.            for j in range(colunas):#percorrer as colunas
  38.                print  j
  39.                item = QtGui.QTableWidgetItem(data[i][j])
  40.                self.table_widget.setItem(i, j, item)
  41.         # now sort
  42.         self.table_widget.sortByColumn(0, QtCore.Qt.DescendingOrder)
  43.  
  44.  
  45. if __name__ == "__main__":
  46.     app = QtGui.QApplication(sys.argv)
  47.     wnd = MainWindow()
  48.     wnd.resize(640, 480)
  49.     wnd.show()
  50.     sys.exit(app.exec_())