本節我們暫時不考慮數據庫的問題,我們將在下一節引入數據庫。
我們需要實現的是新建、編輯、刪除三個action的功能,分別對應着newAction_def
、editAction_def
和delAction_def
三個函數。
但在實現它們之前,我們需要先實現一個新建和編輯功能都需要用到的編輯框,對應一個彈出編輯框的函數showDialog()
;還需要實現一個報錯的提示框,在用戶沒有選擇任何一行卻點擊了修改或刪除時提示用戶”出錯啦”,對應一個彈出提示框的函數showHint()
。
showDialog()
def showDialog(self, ws = '', un = '', pw = '', url = ''):
edit_dialog = QtGui.QDialog(self)
group = QtGui.QGroupBox('Edit Info', edit_dialog)
lbl_website = QtGui.QLabel('Website:', group)
le_website = QtGui.QLineEdit(group)
le_website.setText(ws)
lbl_username = QtGui.QLabel('Username:', group)
le_username = QtGui.QLineEdit(group)
le_username.setText(un)
lbl_password = QtGui.QLabel('Password:', group)
le_password = QtGui.QLineEdit(group)
le_password.setText(pw)
lbl_url = QtGui.QLabel('Url:', group)
le_url = QtGui.QLineEdit(group)
le_url.setText(url)
ok_button = QtGui.QPushButton('OK', edit_dialog)
cancel_button = QtGui.QPushButton('CANCEL', edit_dialog)
ok_button.clicked.connect(edit_dialog.accept)
ok_button.setDefault(True)
cancel_button.clicked.connect(edit_dialog.reject)
group_layout = QtGui.QVBoxLayout()
group_item = [lbl_website, le_website,
lbl_username, le_username,
lbl_password, le_password,
lbl_url, le_url]
for item in group_item:
group_layout.addWidget(item)
group.setLayout(group_layout)
group.setFixedSize(group.sizeHint())
button_layout = QtGui.QHBoxLayout()
button_layout.addWidget(ok_button)
button_layout.addWidget(cancel_button)
dialog_layout = QtGui.QVBoxLayout()
dialog_layout.addWidget(group)
dialog_layout.addLayout(button_layout)
edit_dialog.setLayout(dialog_layout)
edit_dialog.setFixedSize(edit_dialog.sizeHint())
if edit_dialog.exec_():
website = le_website.text()
username = le_username.text()
password = le_password.text()
url = le_password.text()
return True, website, username, password, url
return False, None, None, None, None
我們使用QGroupBox類來在一個dialog中放置多個標籤、編輯框。
這段代碼主要是一些佈局上的調整,很好理解。如果沒有學過PyQt4中的佈局,可以參見我翻譯的一篇國外網站的很好的教程中關於佈局的那一節:
PyQt4入門教程(4)_佈局管理:http://blog.csdn.net/bigbennyguo/article/details/50721991
主要講三個方面。
1.這個函數有參數、返回值。返回值大家很好理解,爲了得到用戶輸入的內容。參數則是爲了實現編輯功能,當彈出編輯框時,已有的數據會通過參數的方式傳給我們的dialog,使之在文本框中展現出來,我們後面就會看到。
2.
ok_button.clicked.connect(edit_dialog.accept)
ok_button.setDefault(True)
cancel_button.clicked.connect(edit_dialog.reject)
我們創建的傳統dialog是沒有按鈕的,這裏我們需要將自己創建的ok和cancel按鈕和dialog的兩個相應的槽聯繫起來。
3.
if edit_dialog.exec_():
只有我們輸入數據之後點擊‘ok’按鈕,edit_dialog.exec()返回值纔是True,點擊‘cancel’會使它的返回值爲False。這樣的話,只有返回True,我們才使函數返回用戶輸入的一系列內容。
除了這三點之外,大家有什麼不懂的地方可以在留言區提問。
showHint()
def showHint(self):
hint_msg = QtGui.QMessageBox()
hint_msg.setText('No selected row!')
hint_msg.addButton(QtGui.QMessageBox.Ok)
hint_msg.exec_()
沒什麼需要解釋的,可能佈局有些問題,大家自己調整一下。
接下來,我們就分別看這三個功能如何實現。
newAction_def()
def newAction_def(self):
data = self.showDialog()
if data[0]:
self.current_row += 1
self.grid.insertRow(self.current_row - 1)
for i in range(4):
new_item = QtGui.QTableWidgetItem(data[i + 1])
self.grid.setItem(self.current_row - 1, i, new_item)
我們在類的構造函數中定義了self.current_row=0,現在有用了。
大家注意區分行數和行號,行數=行號+1,所以在insert row和set item的時候要寫self.current_row-1。
if data[0]:
data[0]是ok,也就是用戶是否點擊了確認。
editAction_def()
def editAction_def(self):
selected_row = self.grid.selectedItems()
if selected_row:
edit_row = self.grid.row(selected_row[0])
old_data = []
for i in range(4):
old_data.append(self.grid.item(edit_row, i).text())
new_data = self.showDialog(*old_data)
if new_data[0]:
for i in range(4):
new_item = QtGui.QTableWidgetItem(new_data[i + 1])
self.grid.setItem(edit_row, i, new_item)
else:
self.showHint()
selected_row = self.grid.selectedItems()
if selected_row:
edit_row = self.grid.row(selected_row[0])
這幾句用來讀取用戶選擇的行數,具體這些函數的返回值都是什麼,請見第一節中提到的PyQt4安裝後自帶文檔。
new_data = self.showDialog(*old_data)
這裏解釋一下這裏的星號。星號是一種操作符,這裏表示把一個列表(或元組)拆分成一個個項目輸入函數,而不是以列表(元組)的整體輸入。
這裏我們就用到了showDialog()的參數。
delAction_def()
def delAction_def(self):
selected_row = self.grid.selectedItems()
if selected_row:
del_row = self.grid.row(selected_row[0])
self.grid.removeRow(del_row)
self.current_row -= 1
else:
self.showHint()
delete的功能算是三個裏面最簡單的啦:找到選擇的那一條信息,刪掉就可以了。怎樣知道用戶選擇了哪一行呢?和edit函數中是一樣的。
本節收工
這裏運行一下程序,是不是已經可以自由添加、修改、刪除信息了呢?但是美中不足的是,每次關閉程序後,數據就會消失,再次打開程序又是一片空白……下面一節我們就會引入數據庫解決這個問題。
【持續更新中,歡迎關注!】