# 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_()
PyQt有数据库支持(我个人没有使用过,所以我不能评论),但是如果你看一下QDatabase,它应该是非常简单的文档。如果应用程序的api总是可以访问Qt,那么这可能是最好的方法,因为它们还有一些用于映射到接口的其他模型。在
另一种选择是使用pythonorm(对象关系映射器),如Django、SQLAlchemy或storm,并定义表(模型)并手动将它们加载到设计器界面中。在
我个人的做法是,实际上我已经建立了自己的ORM名为ORB和一个名为ProjexUI的PyQt扩展库。ORB库是Qt独立的,因此可以在非Qt项目中使用,ProjexUI库包含有助于在Qt小部件中使用数据库记录的映射。在
有关文档和更多信息,请查看:http://www.projexsoftware.com
对于一个简单的示例,请通过执行以下操作来创建新的UI文件:
这将创建PyQt接口部分,接下来您仍然需要将接口连接到数据库后端。如何使用ORB执行此操作的最简单示例是:
相关问题 更多 >
编程相关推荐