PyQt连接到Postgresql并显示值

2024-10-01 07:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和PyQt的新手。我想知道如何使用PyQt连接到postgresqltb并将其显示在网格窗口中。我用的是Qt设计器。有人能帮我吗?谢谢您。在

谨致问候, 奈特汉


Tags: 网格qtpyqt新手问候postgresqltb
1条回答
网友
1楼 · 发布于 2024-10-01 07:10:50

PyQt有数据库支持(我个人没有使用过,所以我不能评论),但是如果你看一下QDatabase,它应该是非常简单的文档。如果应用程序的api总是可以访问Qt,那么这可能是最好的方法,因为它们还有一些用于映射到接口的其他模型。在

另一种选择是使用pythonorm(对象关系映射器),如Django、SQLAlchemy或storm,并定义表(模型)并手动将它们加载到设计器界面中。在

我个人的做法是,实际上我已经建立了自己的ORM名为ORB和一个名为ProjexUI的PyQt扩展库。ORB库是Qt独立的,因此可以在非Qt项目中使用,ProjexUI库包含有助于在Qt小部件中使用数据库记录的映射。在

有关文档和更多信息,请查看:http://www.projexsoftware.com

对于一个简单的示例,请通过执行以下操作来创建新的UI文件:

  • 加载Qt设计器
  • 创建新的Qt对话框
  • 拖放QTreeWidget
  • 右键单击并执行“更改对象名…”gt;“uiOrbTREE”
  • 右键单击小部件并选择“升级到…”
    • 基类名称保持不变(QTreeWidget)
    • 将“Promoted class name”设置为“XOrbTreeWidget”
    • 将“头文件”设置为'projexui.widgets.xorbtreewidget'
    • 单击“添加”,然后单击“升级”
  • 选择“基础”对话框,然后单击“在网格中布局”
  • 保存UI文件(在代码中我将其称为UI_文件)

这将创建PyQt接口部分,接下来您仍然需要将接口连接到数据库后端。如何使用ORB执行此操作的最简单示例是:

# import the projexui and orb libraries
import projexui
import orb

# import PyQt modules
import PyQt4.QtGui
import PyQt4.uic

# create our database model
class User(orb.Table):
    __db_columns__ = [
        orb.Column( orb.ColumnType.String, 'username' ),
        orb.Column( orb.ColumnType.String, 'password' ),
        orb.Column( orb.ColumnType.Boolean, 'isActive' )
    ]

# the above model will by default create a PostgreSQL table called default_user with
# the fields _id, username, password and is_active.  All of this is configurable, but
# you should read the docs for more info

# create the database information
db = orb.Database('Postgres', DATABASE_NAME) # set your db name
db.setUsername(USER_NAME) # set your db user name
db.setPassword(PASSWORD)  # set your password
db.setHost('localhost')   # set your host
db.setPort(5432)          # set your port

# register the database
orb.Orb.instance().registerDatabase(db)

# sync the datbase (this will create your tables, update columns, etc.)
# NOTE: this should not always be called, it is here as an example
db.sync( dryRun = False ) # dryRun will just output the SQL calls

#              -
# End Database Code
#              -

class ExampleDialog(QtGui.QDialog):
    def __init__( self, parent = None ):
        super(ExampleDialog, self).__init__(parent)

        # load your UI file
        PyQt4.uic.loadUi(UI_FILE, self) # use the UI_FILE you saved

        # connect the tree to the model
        self.uiOrbTREE.setTableType(User)

        # that is all you have to do to link the tree to the model and vice-versa,
        # this is the most simple example of how to do this - you can do more as far
        # as linking queries and sorting and such, but you should read the docs on
        # the site

# launch as an application
if ( __name__ == '__main__' ):
    # always check for an existing application first!
    app = None
    if ( not QtGui.QApplication.instance() ):
        app = QtGui.QApplication(sys.argv)

    dialog = ExampleDialog()
    dialog.show()

    # execute the app if we created it
    if ( app ):
        app.exec_()

相关问题 更多 >