做外贸soho 需要有网站吗/网络营销的专业知识
本节我们暂时不考虑数据库的问题,我们将在下一节引入数据库。
我们需要实现的是新建、编辑、删除三个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, urlreturn 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 += 1self.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 -= 1else:self.showHint()
delete的功能算是三个里面最简单的啦:找到选择的那一条信息,删掉就可以了。怎样知道用户选择了哪一行呢?和edit函数中是一样的。
本节收工
这里运行一下程序,是不是已经可以自由添加、修改、删除信息了呢?但是美中不足的是,每次关闭程序后,数据就会消失,再次打开程序又是一片空白……下面一节我们就会引入数据库解决这个问题。
【持续更新中,欢迎关注!】