Using QT Designer to create TableView to Postgres Database

谁说胖子不能爱 提交于 2019-12-02 09:43:30

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!