Using QT Designer to create TableView to Postgres Database

后端 未结 1 1437
星月不相逢
星月不相逢 2021-01-27 02:35

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 r

相关标签:
1条回答
  • 2021-01-27 03:13

    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.

    0 讨论(0)
提交回复
热议问题