title: Using QT Designer to create TableView to Postgres Database
body: <p>I'm creating a plugin in Quantum GIS that is using Postgres as the back end and QT Designer to make the GUI. I'm using psycopg2 to run scripts in the database and even fetch results of queries to set the values of labels in the GUI. This stuff is working fine for me.</p>
  
  <p>What I would like to do now after some queries are run by clicking a 'calculate' button is for the resulting table to be shown in the plugin as a TableView. I know there widget exists expressly for the purpose of viewing tables but I can't quite figure out how to go about it. I'm not sure if I should be using psycopg2 or PySide, since most examples I have seen online use the latter. </p>
  
  <p>I am wondering if someone can tell me which between psycopg2 and PySide should be used to create the TableView. Second, I am wondering what the 'signal' should be to the TableView widget to display the results of a query in Postgres. Lastly, is anyone can offer some instruction as to how to set up the code it would be hugely appreciated!</p>
  
  <p>Cheers,</p>
  
  <p>Roman</p>
  
  <p>I've gone ahead and tried following the PyQt documentation, but as it's provided in C++ and I'm only a beginner programmer using Python I'm not sure if I've caught all the necessary amendments to the code syntax. Anyways, this is what I have so far:</p>
  
  <pre><code>db = QSqlDatabase.addDatabase("database")
    db.setHostName("localhost")
    db.setUserName("postgres")
    db.setPassword("password")
    #Not sure what to do to set the connection. The C++ documentation says to put "bool ok = db.open();"
  
    model = QSqlQueryModel()
    model.setQuery("SELECT name, density, deveff FROM public." +str(filename)+ "_rezoning ORDER BY gid;")
    model.setHeaderData(0, Qt.Horizontal, "Name")
    model.setHeaderData(1, Qt.Horizontal, "Density")
    model.setHeaderData(2, Qt.Horizontal, "DevEff")
  
    view = QTableView()
    view.setModel(model)
    view.show()
  </code></pre>
  
  <p>What happens when I click the button in my GUI to run the calculations, a small blank QGIS window briefly flashes and goes away. At least I'm not getting an error, but it's obviously not complete. I assume part of the issue is the connection to the database that is missing and that I do not know how to set. The other issue is that I would like this to show in the tableView widget in the GUI, but I'm not sure how to specify this...</p>
  
  <p>Any further tips? I really appreciate it.</p>
  
  <p>Roman</p>