Using QT Designer to create TableView to Postgres

2020-05-08 12:04发布

问题:

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.

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.

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!

Cheers,

Roman

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:

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()

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...

Any further tips? I really appreciate it.

Roman

回答1:

If you're planning to use Qt widgets and models, PySide (PyQt, or plain Qt/C++) is the way to go.

With bare psycopg2 you'll have a lot more work to do, and you'll need to implement your own model in order to leverage Qt's model/view classes. This is simply not the Qt way of doing things. PySide (and PyQt) has it own means to connect to a supported database, there's no need for pure Python database adapters like psycopg2. It uses the underlying libqt4-sql library (C++) and the installed plugins (QPSQL, QMYSQL, QSQLITE, etc).

Essentially you need to:

  1. Connect to a database.
  2. Instantiate a model (QSqlQueryModel, QSqlTableModel or a custom QAbstractTableModel derived class)
  3. Attach that model to a view (ie. QTableView).

Take a look at the PySide QtSql Documentation and the PyQt documentation to get an idea. They're mostly compatible/interchangeable, but at a glance I see that the PyQt documentation looks more complete.

EDIT (after your edit): A Qt GUI application requires an event loop to run, and that's provided by a QApplication instance. Before going any further with the specifics of your app, take the time to understand a few basic concepts first. Here's a nice Getting Started with PyQt Guide.