Python PyQt连接到Postgresql并显示值
我是Python和PyQt的新手。我想知道如何使用PyQt连接到postgresqldb并将其显示在网格窗口中。我正在使用Qt设计器。有人能帮我吗?多谢各位 致以最良好的祝愿, NethanPyQt有数据库支持(我个人没有使用过,所以我不能对此发表评论),但如果您查看QDatabase,它应该是非常简单的文档。如果应用程序的api始终可以访问Qt,那么这可能是最好的方法,因为它们还有一些用于映射到接口的附加模型 另一种选择是使用PythonORM(对象关系映射器),如Django、SQLAlchemy或storm,定义表的(模型)并手动将它们加载到设计器界面中 我个人的做法是,实际上我已经建立了自己的ORM ORB和PyQt扩展库ProjexUI。ORB库是独立于Qt的,因此可以在非Qt项目中使用,而ProjexUI库包含帮助在Qt小部件中使用数据库记录的映射 有关文档和更多信息,请查看: 对于一个简单的示例,通过执行以下操作创建一个新的UI文件:Python PyQt连接到Postgresql并显示值,python,postgresql,pyqt,Python,Postgresql,Pyqt,我是Python和PyQt的新手。我想知道如何使用PyQt连接到postgresqldb并将其显示在网格窗口中。我正在使用Qt设计器。有人能帮我吗?多谢各位 致以最良好的祝愿, NethanPyQt有数据库支持(我个人没有使用过,所以我不能对此发表评论),但如果您查看QDatabase,它应该是非常简单的文档。如果应用程序的api始终可以访问Qt,那么这可能是最好的方法,因为它们还有一些用于映射到接口的附加模型 另一种选择是使用PythonORM(对象关系映射器),如Django、SQLAlch
- 加载Qt设计器
- 创建一个新的Qt对话框
- 拖放QTreeWidget
- 右键单击并执行“更改对象名称…”>“uiOrbTREE”
- 右键单击小部件并选择“升级到…”
- 保持基类名称不变(QTreeWidget)
- 将“提升的类名”设置为“XOrbTreeWidget”
- 将“头文件”设置为“projexui.widgets.xorbtreewidget”
- 单击“添加”,然后单击“升级”
- 选择“基础”对话框并单击“在网格中布局”
- 保存UI文件(我将在代码中将其称为UI_文件)
# 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_()
更具体地说,您通过代码尝试了什么?Qt Designer除了制作布局外,与此无关。