Bonjour,

Je suis sous Phython 2.7 et PyQt4.

J'ai une table dans postgresql qui contient 3916 lignes qui correspondent aux non de réferentiels spatiaux (srtext) et au code du référentiel (srid).
J'ai besoin de choisir un référentiel pour cela j'ai afficher cette table dans un QtableWidget via une requete sql, je n'ai donc que à faire un signal/slot lorsque je selectionne un ligne. J'ai des erreurs d'affichage de ma table lors que je run mon appli. Le but est de récupérer le code srid pour l'utiliser après dans ma base de donnée Postgis.

#1 J'ai bien 3916 lignes corespondant à srtext, mais l'orde dans lequel elles s'affichent n'a rien à voir avec la table de ma base de donnée
#2 Je n'arrive pas à remplir ma seconde colonnes srid, c'est vide.

Ps: Au dépard j'avais choisi un Qcombox pour afficher mes lignes et j'ai eu le même problème (voir la discution )

Voici mon code pour ceux qui aurait des idées :

MErci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
# -*- coding: utf-8 -*-
import psycopg2
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, Qt
 
 
con = None
 
class MainWindow(QtGui.QTableWidget):
 
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        layout = QtGui.QVBoxLayout()
 
        self.table_widget = QtGui.QTableWidget() # Créer la table
        self.connect(self.table_widget,SIGNAL('cellClicked(int, int)'), self.returnIndex) # Return la ligne
        self.sridData() # Lance le remplissage
 
        layout.addWidget(self.table_widget)
        self.setLayout(layout)
 
    def sridData(self): ##REMPLISSAGE
        try:
            conn = psycopg2.connect("dbname='postgis_21_sample' user='postgres' host='localhost' password='postgresql'")
        except:
            print "I am unable to connect to the database"
        cur = conn.cursor()        
        self.data= cur.execute("SELECT srtext, srid FROM spatial_ref_sys;")
        data = cur.fetchall()
 
        lignes = len(data)
        columns =  len(data[0])
        i = 0
        j = 0
 
        self.table_widget.setRowCount(lignes)
        self.table_widget.setColumnCount(columns)
        self.table_widget.setHorizontalHeaderLabels(['Label sird', 'srid'])
        #self.table_widget.setColumnWidth(1, 80)
        self.table_widget.horizontalHeader().setResizeMode(0, QHeaderView.Stretch)
 
        for i in range(lignes):
            for j in range(columns):
                item = QtGui.QTableWidgetItem(data[i][j])
                self.tabledata = self.table_widget.setItem(i, j, item)
        #self.table_widget.sortByColumn(0, QtCore.Qt.AscendingOrder) # permet de choisir l'ordre d'affichage
 
    def returnIndex(self,row,column):
        row =row +1
        try:
            conn = psycopg2.connect("dbname='postgis_21_sample' user='postgres' host='localhost' password='postgresql'")
        except:
            print "I am unable to connect to the database"
        cur = conn.cursor()        
        self.data= cur.execute("SELECT srid FROM spatial_ref_sys;")
        srid = cur.fetchall()
        srid = srid[row]
        print row, srid
 
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    wnd = MainWindow()
    wnd.resize(900, 500)
    wnd.show()
    sys.exit(app.exec_())