我正在尝试在行编辑中有一个完成者,其中一个放入一些字符串,如“firstname,lastname”,每个字符串对应一个表模型中的两列之一(最终目标是要灵活,但目前我只想要这个运行)。我目前试图解决的方法是:
a)将两列的结果合并到一个代理wa第三列,它只有firstname,lastname作为离散字符串(如果用户将其作为lastname,firstname放入,则不方便。当前实现不起作用bc当我尝试在“假”列上执行setCompletionColumn时,它不会激活我重新实现的数据方法.columnCount包含假列tho)
b)有一个不过滤的完成符,后端代理模型的acceptrow()过滤器在第一个和最后一个过滤器(不知道如何进行非过滤完成 - 否则它只是查看一列并且最后的弹出窗口显示了一堆没有姓氏的名字。
c)欺骗树模型,并且当我放一个逗号时,模型然后查看一个“分支”,该分支由所有人的首个OR姓氏(以?开头?)前逗号字符串组成。 (我只是不确定如何开始这个,它使用与b相同类型的接受)但idk如何做单独的〜分支〜)这让我很头疼,这些实现都不是特别令人愉快。我是不是对自己太过努力并且喜欢使用错误的工具,或者这只是我必须要扣入的东西?我可以粘贴一些代码,但是我想知道我在做什么甚至是值得的。
更新:代码片段和pix
] 1
红色圆圈是我正在谈论的多个名字,它们对应于表格中的不同条目,但完成者仅限于一列。在点击和获取用户的名字方面我已经得到了大部分想法,它只是格式和处理逗号。
这是我的完成课程的一部分:
class CustomQCompleter(QCompleter): #dropdown box of suggestions
def __init__(self, parent=None,*args):#parent=None):
self.columnind=0
super(CustomQCompleter, self).__init__(*args)
self.parent=parent
self.setCompletionColumn(self.columnind)
def pathFromIndex(self,index,role=QtCore.Qt.DisplayRole): #decides what gets put into selection box
#print(index)
model=self.source_model
return model.data(model.index(index.row(),self.columnind),role=QtCore.Qt.DisplayRole)
def splitPath(self, path):
self.local_completion_prefix = path #???
print(path)
sp=self.local_completion_prefix.split(',')
if len(sp)>1: #
print('path split')
return sp
return [path]
这是我重新实现的代理模型的接受:
def filterAcceptsRow(self, row_num, parent): #input matches if this returns true
self.filterString=self.parent().inp.text()
self.filterList=[i.strip() for i in self.filterString.split(',')]
l=len(self.filterList)
while l<2:#easiest way to get thru this atm, clean up later
self.filterList.append('')
l=len(self.filterList)
if self.baseModel is not None:
model=self.baseModel
else:
model = self.sourceModel() # the underlying model,
# implmented as a python array
row = [model.data(model.index(row_num,0)),model.data(model.index(row_num,1))] #gets the data for this row from sql model in list format
#(row[0] starts with fname and row[1] starts w lname) OR vice versa
tests=[row[0].startswith(self.filterList[i]) and row[1].startswith(self.filterList[1-i]) for i in [0,1]]
#tests = len(self.filterSet.intersection(set([row[col] for col in self.filterColumns])))==2
# print(tests)
return True in tests
理想情况下它看起来像这样:
答案 0 :(得分:1)
策略是使用代理创建一个将返回连接文本的新角色,以便看到带有连接文本的弹出窗口,我们将建立popup()
QCompleter
的自定义委托。 1}}。
import sys
from PyQt5.QtCore import QIdentityProxyModel, Qt
from PyQt5.QtWidgets import QStyledItemDelegate, QCompleter, QApplication, QWidget, \
QVBoxLayout, QLineEdit, QTableView, QStyleOptionViewItem, QStyle
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
JoinRole = Qt.UserRole +1
class JoinProxyModel(QIdentityProxyModel):
def __init__(self, columns, *args, **kwargs):
QIdentityProxyModel.__init__(self, *args, **kwargs)
self._columns = columns
def data(self, index, role):
if role == JoinRole:
texts = []
for c in self._columns:
texts.append(self.sibling(index.row(), c, index.parent()).data())
return ", ".join(texts)
return QIdentityProxyModel.data(self, index, role)
class JoinDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
opt = QStyleOptionViewItem(option)
self.initStyleOption(opt, index)
opt.text = index.data(JoinRole)
widget = option.widget
style = widget.style() if widget else QApplication.style()
style.drawControl(QStyle.CE_ItemViewItem, opt, painter, widget)
class JoinCompleter(QCompleter):
def __init__(self, model, columns, parent=None):
QCompleter.__init__(self, parent)
# columns: are the columns that are going to concatenate
proxy = JoinProxyModel(columns)
proxy.setSourceModel(model)
self.setModel(proxy)
self.setCompletionRole(JoinRole)
self.setFilterMode(Qt.MatchContains)
self.popup().setItemDelegate(JoinDelegate(self))
def createConnection():
db = QSqlDatabase.addDatabase("QSQLITE");
db.setDatabaseName(":memory:")
if not db.open():
QMessageBox.critical(nullptr, QObject.tr("Cannot open database"),
QObject.tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox.Cancel)
return False
query = QSqlQuery()
query.exec_("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))")
query.exec_("insert into person values(101, 'Danny', 'Young')")
query.exec_("insert into person values(102, 'Christine', 'Holand')")
query.exec_("insert into person values(103, 'Lars', 'Gordon')")
query.exec_("insert into person values(104, 'Roberto', 'Robitaille')")
query.exec_("insert into person values(105, 'Maria', 'Papadopoulos')")
return True
if __name__ == '__main__':
app = QApplication(sys.argv)
if not createConnection():
sys.exit(-1)
w = QWidget()
lay = QVBoxLayout(w)
le = QLineEdit()
view = QTableView()
model = QSqlTableModel()
model.setTable("person")
model.select()
completer = JoinCompleter(model, [1, 2])
le.setCompleter(completer)
view.setModel(model)
lay.addWidget(le)
lay.addWidget(view)
w.show()
sys.exit(app.exec_())