0

我正在尝试在行编辑中完成一个完成,其中一个输入一些字符串,例如“名字,姓氏”,每个字符串对应于表模型中的两列之一(最终目标是为了灵活,但目前我只想运行这个)。我目前尝试的方法是:

a) 将两列的结果合并到代理 wa 第三列,其中只有名字,姓氏作为离散字符串(如果用户将其作为姓氏,名字输入,这很不方便。当我尝试时,当前的实现不起作用 bc在“假”列上执行 setCompletionColumn 它不会激活我重新实现的数据方法。columnCount 包括假列)

b)有一个不过滤的完成器,带有一个后端代理模型,其接受行()过滤器过滤第一个和最后一个(不知道如何执行非过滤完成器 - 否则它只是查看一列和弹出窗口最后显示一堆没有姓氏的名字)

c) 欺骗 Tree 模型并制作它,当我放一个逗号时,该模型会查看一个“分支”,该“分支”由所有名字或姓氏为(开头?)逗号前字符串的人组成。(我只是不确定如何开始,它使用与 b 相同的接受行)但我不知道如何做像单独的〜分支〜)

这让我很头疼,而且这些实现都不是特别令人愉快的。我是不是对自己太苛刻了,喜欢使用错误的工具,还是只是我必须努力解决的问题?我可以粘贴一些代码,但 atm 我想知道我所做的是否值得。


更新:代码片段和像素

![在此处输入图像描述] 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   

理想情况下,它看起来像这样:

在此处输入图像描述

4

1 回答 1

1

策略是使用一个代理来创建一个新角色,该角色将返回连接文本,以便看到带有连接文本的弹出窗口,我们将为 of 建立一个自定义popup()委托QCompleter

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_())
于 2018-06-13T05:23:59.930 回答