使用 QT Designer 创建 TableView 到 Postgres 数据库 [英] Using QT Designer to create TableView to Postgres Database

查看:46
本文介绍了使用 QT Designer 创建 TableView 到 Postgres 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Quantum GIS 中创建一个插件,该插件使用 Postgres 作为后端和 QT Designer 来制作 GUI.我正在使用 psycopg2 在数据库中运行脚本,甚至获取查询结果以在 GUI 中设置标签的值.这些东西对我来说很好用.

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.

在通过单击计算"按钮运行某些查询之后,我现在想做的是将结果表作为 TableView 显示在插件中.我知道有小部件是为了查看表格而明确存在的,但我不太清楚如何去做.我不确定我应该使用 psycopg2 还是 PySide,因为我在网上看到的大多数示例都使用后者.

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.

我想知道是否有人可以告诉我应该使用 psycopg2 和 PySide 之间的哪个来创建 TableView.其次,我想知道 TableView 小部件的信号"应该是什么,以在 Postgres 中显示查询结果.最后,任何人都可以提供一些有关如何设置代码的说明,将不胜感激!

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!

干杯,

罗马

我已经继续尝试遵循 PyQt 文档,但由于它是在 C++ 中提供的,而且我只是一个使用 Python 的初学者程序员,我不确定我是否已经掌握了对代码语法的所有必要修改.无论如何,这就是我目前所拥有的:

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

当我单击 GUI 中的按钮运行计算时会发生什么,一个小的空白 QGIS 窗口会短暂闪烁并消失.至少我没有收到错误消息,但它显然不完整.我认为问题的一部分是与数据库的连接丢失并且我不知道如何设置.另一个问题是我希望它显示在 GUI 的 tableView 小部件中,但我不确定如何指定它...

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.

罗马

推荐答案

如果您计划使用 Qt 小部件和模型,PySide(PyQt,或普通 Qt/C++)是您的最佳选择.

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

使用裸psycopg2,您将有更多工作要做,并且您需要实现自己的模型以利用Qt 的模型/视图类.这根本不是 Qt 的做事方式.PySide(和 PyQt)有自己的方法来连接到支持的数据库,不需要像 psycopg2 这样的纯 Python 数据库适配器.它使用底层的 libqt4-sql 库(C++)和已安装的插件(QPSQL、QMYSQL、QSQLITE 等).

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

基本上你需要:

  1. 连接到数据库.
  2. 实例化模型(QSqlQueryModel、QSqlTableModel 或自定义 QAbstractTableModel 派生类)
  3. 将该模型附加到视图(即 QTableView).

查看 PySide QtSql 文档PyQt 文档 以获得一个想法.它们大多是兼容/可互换的,但乍一看,我发现 PyQt 文档看起来更完整.

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.

编辑(编辑后):Qt GUI 应用程序需要一个事件循环来运行,这是由 QApplication 实例提供的.在进一步了解您的应用程序的细节之前,先花点时间了解一些基本概念.这是一个不错的PyQt 入门指南.

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.

这篇关于使用 QT Designer 创建 TableView 到 Postgres 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆