[PyQt6] PartⅡ. CRUD with QListWidget
增删改查
创建一个程序完成功能
QLineEdit.EchoMode.Normal: 4种模式
QInputDialog的getText()函数弹出标准字符串输入对话框,getText()函数原型如下:
# 指向父窗口
QString QInputDialog::getText(QWidget parent,
# title is the text which is displayed in the title bar of the dialog
const QString &title,
# label is the text which is shown to the user (it should say what should be entered)
const QString &label,
# text is the default text which is placed in the line edit
QLineEdit::EchoMode mode = QLineEdit::Normal,
# mode is the echo mode the line edit will use
**const QString &text* = QString(),
# If ok is nonnull *ok will be set to true if the user pressed OK and to false if the user pressed Cancel. The dialog's parent is parent. The dialog will be modal and uses the specified widget flags.
bool *ok = nullptr,
# 指定控件的窗口标识的,值为0即Qt::Widget,一个枚举类型,在Qt Assistant里面搜索 Qt::WindowFlags就会出现其详细的解释
Qt::WindowFlags flags = Qt::WindowFlags(),
# inputMethodHints is the input method hints that will be used in the edit widget if an input method is active
Qt::InputMethodHints inputMethodHints = Qt::ImhNone)
QMessageBox.question:Opens a question message box with the given title and text in front of the specified parent widget.
reply = QMessageBox.question(self,
"Remove Item", # 标题
"Do you want to Remove Item? ", # 提示
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) # 确认 | 取消
if reply == QMessageBox.StandardButton.Yes:
self.listWidget.takeItem(row) # 删除指定元素
关键代码分析
self.pushButton_add.clicked.connect(self.add_item) # 绑定点击事件,若鼠标点击按钮,即发送signal,到slot(add_item)
# self【必须】: 表示父窗口为当前对话框;title【必须】:标题名;title【必须】:提示信息;QLineEdit.EchoMode.Normal:表明当前显示模式为正常模式,item.text():表明默认数据为原数据
data, ok = QInputDialog.getText(self, title, title, QLineEdit.EchoMode.Normal, item.text())
def remove_item(self):
row = self.listWidget.currentRow()
item = self.listWidget.item(row)
if item is None:
return
reply = QMessageBox.question(self,
"Remove Item", # 标题
"Do you want to Remove Item? ", # 提示
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) # 确认 | 取消
if reply == QMessageBox.StandardButton.Yes:
self.listWidget.takeItem(row) # 删除指定元素
完整代码
# Form implementation generated from reading ui file 'listWidgetApp.ui'
#
# Created by: PyQt6 UI code generator 6.3.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QDialog, QInputDialog, QLineEdit, QMessageBox
class Ui_Form(QDialog):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(697, 475)
self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
self.horizontalLayout.setObjectName("horizontalLayout")
self.listWidget = QtWidgets.QListWidget(Form)
self.listWidget.setObjectName("listWidget")
self.horizontalLayout.addWidget(self.listWidget)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton_add = QtWidgets.QPushButton(Form)
self.pushButton_add.setObjectName("pushButton_add")
# connect signal
self.pushButton_add.clicked.connect(self.add_item) # 绑定点击事件,若鼠标点击按钮,即发送signal,到slot(add_item)
self.verticalLayout.addWidget(self.pushButton_add)
self.pushButton_edit = QtWidgets.QPushButton(Form)
self.pushButton_edit.setObjectName("pushButton_edit")
# connect signal
self.pushButton_edit.clicked.connect(self.edit_item)
self.verticalLayout.addWidget(self.pushButton_edit)
self.pushButton_remove = QtWidgets.QPushButton(Form)
self.pushButton_remove.setObjectName("pushButton_remove")
# connect signal
self.pushButton_remove.clicked.connect(self.remove_item)
self.verticalLayout.addWidget(self.pushButton_remove)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
self.verticalLayout.addItem(spacerItem)
self.pushButton_sort = QtWidgets.QPushButton(Form)
self.pushButton_sort.setObjectName("pushButton_sort")
# connect signal
self.pushButton_sort.clicked.connect(self.sort_item)
self.verticalLayout.addWidget(self.pushButton_sort)
self.horizontalLayout.addLayout(self.verticalLayout)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def add_item(self): # 添加功能
row = self.listWidget.currentRow() # current为当前item的所在行,意思是当前所选中的item是哪一行,则currentRow为哪一行
title = "Add Item"
data, ok = QInputDialog.getText(self, title, title) # self【必须】: 表示父窗口为当前对话框,title【必须】:标题名,title【必须】:提示信息
if ok and data is not None: # 如果
self.listWidget.insertItem(row, data) #
def edit_item(self):
row = self.listWidget.currentRow() # 通过信号,获取行号
item = self.listWidget.item(row) # 以行号为索引,获取item
if item is not None:
title = "Edit Item"
# self【必须】: 表示父窗口为当前对话框;title【必须】:标题名;title【必须】:提示信息;QLineEdit.EchoMode.Normal:表明当前显示模式为正常模式,item.text():表明默认数据为原数据
data, ok = QInputDialog.getText(self, title, title, QLineEdit.EchoMode.Normal, item.text())
if ok and data is not None: # 如果这两个都不为空
item.setText(data)
def remove_item(self):
row = self.listWidget.currentRow()
item = self.listWidget.item(row)
if item is None:
return
reply = QMessageBox.question(self,
"Remove Item", # 标题
"Do you want to Remove Item? ", # 提示
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) # 确认 | 取消
if reply == QMessageBox.StandardButton.Yes:
self.listWidget.takeItem(row) # 删除指定元素
def sort_item(self):
self.listWidget.sortItems()
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton_add.setText(_translate("Form", "Add"))
self.pushButton_edit.setText(_translate("Form", "Edit"))
self.pushButton_remove.setText(_translate("Form", "Remove"))
self.pushButton_sort.setText(_translate("Form", "Sort"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())
程序截图
reference
QTableWidget中的currentRow的一个误区(currentRow是当前所选中的item的所在行)
Qt 编程 —— 输入对话框(QInputDialog)的使用 【学习笔记】
Qt::WindowFlags枚举类型解析
QT Documention