2012-07-18 55 views
1

我是Python和PyQt的新手。我想知道如何使用PyQt連接到Postgresql DB並將其顯示到網格窗口。我正在使用Qt Designer。有人可以幫助我嗎?謝謝。PyQt連接到Postgresql並顯示值

最好的問候, Nethan

+0

更具體地說,你通過代碼嘗試了什麼? Qt設計器在做出佈局之外與此無關。 – jdi 2012-07-19 04:01:04

回答

1

的PyQt有數據庫支持(我個人沒有使用過,所以我不能評論),但應該是非常簡單的文檔,如果你看一下QDatabase。如果你的應用程序的api總是可以訪問Qt,這可能是最好的方法,因爲它們還有一些額外的模型來映射到接口。

另一種選擇是使用Python ORM(對象關係映射器),如Django,SQLAlchemy或storm,並定義您的表(模型)並手動將它們加載到您的設計器界面中。

我個人這樣做的方式是,我實際上構建了自己的稱爲ORB的ORM和名爲ProjexUI的PyQt擴展庫。 ORB庫是Qt獨立的,所以它可以在非Qt項目中使用,並且ProjexUI庫包含映射來幫助在Qt小部件中使用數據庫記錄。

對於文檔和更多信息,請訪問:http://www.projexsoftware.com

舉個簡單的例子,做創建一個新的UI文件:

  • 負荷Qt設計
  • 創建一個新的Qt對話框
  • 拖動&刪除QTreeWidget
  • 右鍵單擊並執行「更改objectName ...」>'uiOrbTREE'
  • 在小部件上單擊鼠標右鍵,選擇「提升爲...」
    • 離開基地的類名相同(QTreeWidget)
    • 集「促進類名稱」到「XOrbTreeWidget」
    • 設置「頭文件」到‘projexui.widgets.xorbtreewidget’
    • 點擊‘添加’,然後‘推廣’
  • 選擇基本對話框,然後單擊‘在網格’
  • 保存了UI文件佈局(我會重新將其作爲UI_FILE中的代碼)

這會創建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_()