Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import psycopg2
- import sys
- from PyQt4 import QtCore, QtGui
- con = None
- class MainWindow(QtGui.QWidget):
- def __init__(self, parent=None):
- super(MainWindow, self).__init__(parent)
- self.table_widget = QtGui.QTableWidget()
- self.button = QtGui.QPushButton('Populate')
- self.button.clicked.connect(self.populate)
- layout = QtGui.QVBoxLayout()
- layout.addWidget(self.table_widget)
- layout.addWidget(self.button)
- self.setLayout(layout)
- self.populate()
- def populate(self):
- con = psycopg2.connect(database='testdb', user='Arthur')
- cur = con.cursor()
- cur.execute('SELECT title,fname FROM customer')
- data = cur.fetchall()
- linhas = len(data)
- colunas = len(data[0])
- i = 1
- j = 0
- self.table_widget.setSortingEnabled(True)
- self.table_widget.setRowCount(linhas)
- self.table_widget.setColumnCount(colunas)
- self.table_widget.setHorizontalHeaderLabels(['Titulo', 'Nome'])
- for i in range(linhas):#percorrer as linhas
- for j in range(colunas):#percorrer as colunas
- print j
- item = QtGui.QTableWidgetItem(data[i][j])
- self.table_widget.setItem(i, j, item)
- # now sort
- self.table_widget.sortByColumn(0, QtCore.Qt.DescendingOrder)
- if __name__ == "__main__":
- app = QtGui.QApplication(sys.argv)
- wnd = MainWindow()
- wnd.resize(640, 480)
- wnd.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement