如何在 PyQt5 中为 QTableWidget 中的特定列创建动态自动完成器?
How to Create Dynamic Auto Completer for Particular Column in QTableWidget in PyQt5?
我有一个如下所示的 QTableWidget。我已经使用项目委托为特定的“名称”列添加了自动完成器,效果很好。
给定自动完成列表:['Mark','Riya','Andrew','Thomas','Kevin','Leena','Julie','Peter','Rob','lily','Rambo']
我想让它更有活力。即如果我输入“R”。它给我的值从“R”开始,如快照所示。但我也想获得在任何位置都有“R/r”的值,如“Andrew”、“Peter”。
或者另一种情况是,如果我输入“B”,我应该得到“Rob”、“Rambo”,而不是目前没有任何建议。
我怎样才能使这个工作?
Qt Designer 生成的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1048, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(40, 40, 861, 511))
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed)
self.tableWidget.setRowCount(5)
self.tableWidget.setColumnCount(5)
self.tableWidget.setObjectName("tableWidget")
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 4, item)
self.tableWidget.horizontalHeader().setVisible(True)
self.tableWidget.verticalHeader().setVisible(False)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "ID"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "City"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Number"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Occupation"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 1)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.item(0, 2)
item.setText(_translate("MainWindow", "Newyork"))
item = self.tableWidget.item(0, 3)
item.setText(_translate("MainWindow", "4796423643344"))
item = self.tableWidget.item(0, 4)
item.setText(_translate("MainWindow", "Teacher"))
item = self.tableWidget.item(1, 1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.item(1, 2)
item.setText(_translate("MainWindow", "Chicago"))
item = self.tableWidget.item(1, 3)
item.setText(_translate("MainWindow", "43683284"))
item = self.tableWidget.item(1, 4)
item.setText(_translate("MainWindow", "Nurse"))
self.tableWidget.setSortingEnabled(__sortingEnabled)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我的脚本:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtCore import Qt
from demo import Ui_MainWindow
class TableItemCompleter(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
completionlist = index.data(Qt.UserRole)
autoCompleter = QtWidgets.QCompleter(completionlist,parent)
autoCompleter.setCaseSensitivity(Qt.CaseInsensitive)
editor.setCompleter(autoCompleter)
return editor
class demo_code(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(demo_code, self).__init__()
self.setupUi(self)
self.tableWidget.setItemDelegate(TableItemCompleter())
for rows in range(0,5):
item = QTableWidgetItem('')
item.setData(Qt.UserRole,['Mark','Riya','Andrew','Thomas','Kevin','Leena','Julie','Peter','Rob','lily','Rambo'])
self.tableWidget.setItem(rows, 0, item)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = demo_code()
window.show()
sys.exit(app.exec_())
谢谢。
我有一个如下所示的 QTableWidget。我已经使用项目委托为特定的“名称”列添加了自动完成器,效果很好。
给定自动完成列表:['Mark','Riya','Andrew','Thomas','Kevin','Leena','Julie','Peter','Rob','lily','Rambo']
我想让它更有活力。即如果我输入“R”。它给我的值从“R”开始,如快照所示。但我也想获得在任何位置都有“R/r”的值,如“Andrew”、“Peter”。
或者另一种情况是,如果我输入“B”,我应该得到“Rob”、“Rambo”,而不是目前没有任何建议。
我怎样才能使这个工作?
Qt Designer 生成的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1048, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(40, 40, 861, 511))
self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed)
self.tableWidget.setRowCount(5)
self.tableWidget.setColumnCount(5)
self.tableWidget.setObjectName("tableWidget")
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(0, 4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(1, 4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(2, 4, item)
self.tableWidget.horizontalHeader().setVisible(True)
self.tableWidget.verticalHeader().setVisible(False)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "Name"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "ID"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "City"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "Number"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "Occupation"))
__sortingEnabled = self.tableWidget.isSortingEnabled()
self.tableWidget.setSortingEnabled(False)
item = self.tableWidget.item(0, 1)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.item(0, 2)
item.setText(_translate("MainWindow", "Newyork"))
item = self.tableWidget.item(0, 3)
item.setText(_translate("MainWindow", "4796423643344"))
item = self.tableWidget.item(0, 4)
item.setText(_translate("MainWindow", "Teacher"))
item = self.tableWidget.item(1, 1)
item.setText(_translate("MainWindow", "2"))
item = self.tableWidget.item(1, 2)
item.setText(_translate("MainWindow", "Chicago"))
item = self.tableWidget.item(1, 3)
item.setText(_translate("MainWindow", "43683284"))
item = self.tableWidget.item(1, 4)
item.setText(_translate("MainWindow", "Nurse"))
self.tableWidget.setSortingEnabled(__sortingEnabled)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我的脚本:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtCore import Qt
from demo import Ui_MainWindow
class TableItemCompleter(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
completionlist = index.data(Qt.UserRole)
autoCompleter = QtWidgets.QCompleter(completionlist,parent)
autoCompleter.setCaseSensitivity(Qt.CaseInsensitive)
editor.setCompleter(autoCompleter)
return editor
class demo_code(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(demo_code, self).__init__()
self.setupUi(self)
self.tableWidget.setItemDelegate(TableItemCompleter())
for rows in range(0,5):
item = QTableWidgetItem('')
item.setData(Qt.UserRole,['Mark','Riya','Andrew','Thomas','Kevin','Leena','Julie','Peter','Rob','lily','Rambo'])
self.tableWidget.setItem(rows, 0, item)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = demo_code()
window.show()
sys.exit(app.exec_())
谢谢。