第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何過濾掉單個項目,而不僅僅是在行和列的基礎(chǔ)上?

如何過濾掉單個項目,而不僅僅是在行和列的基礎(chǔ)上?

HUX布斯 2022-01-18 21:01:55
我正在嘗試根據(jù)字符串匹配過濾掉表中的項目。我有一個顯示代理模型以允許過濾的 QTableView,但是如果 (0,0) 和 (1,1) 中的項目與我的字符串匹配但項目 (1,0) 不匹配,它仍將顯示。例如:from PySide.QtGui import *from PySide.QtCore import *class CustomProxyFilter(QSortFilterProxyModel):    def __init__(self):        super(CustomProxyFilter, self).__init__()    def filterAcceptsColumn(self, source_column, parent):        """Re-implementing built-in to hide columns with non matches."""        model = self.sourceModel()        matched_string = self.filterRegExp().pattern().lower()        for row in range(model.rowCount()):            item = model.item(row, source_column)            if item and matched_string in model.item(row, source_column).text().lower():                return True        return Falseclass CustomTableView(QTableView):    """Table view."""    def __init__(self, line_edit):        super(CustomTableView, self).__init__()        custom_model = StandardTableModel()        items = ["apple", "banana", "applebanana"]        for i, item in enumerate(items):            for v, second_item in enumerate(items):                custom_model.setItem(i, v, QStandardItem(item))        self.proxy_model = CustomProxyFilter()        self.proxy_model.setSourceModel(custom_model)        self.setModel(self.proxy_model)        line_edit.textChanged.connect(self.proxy_model.setFilterRegExp)class Window(QWidget):    def __init__(self):        super(Window, self).__init__()        self.setLayout(QVBoxLayout())        self.line_edit = QLineEdit()        self.layout().addWidget(self.line_edit)        self.layout().addWidget(CustomTableView(self.line_edit))我希望會發(fā)生的是,如果我的桌子看起來像a|b|c-----c|a|b按“a”過濾后的結(jié)果表將是a|a我目前的解決方案顯示。a|b---c|a更新其他案例a|a|c-----a|x|b-----c|b|a變成a|a|a-----a這個案例a|a|y|c-------a|a|w|a-------c|a|w|w變成a|a|a|a-----a|a|基本上每個項目都會在可能的情況下向左上角移動。當他們是不同的名字時,他們會像這樣按字母順序排列自己1|2|3|4-------5|6|7|8
查看完整描述

1 回答

?
眼眸繁星

TA貢獻1873條經(jīng)驗 獲得超9個贊

為了實現(xiàn)您的要求,我通過以下方式實現(xiàn)了幾個連接的代理:


model-->Table2ListProxyModel-->QSortFilterProxyModel-->List2TableProxyModel

想法是將其轉(zhuǎn)換為列表的結(jié)構(gòu),因為過濾一行相當于過濾一個項目,相同的代理對其進行排序,然后我們將列表轉(zhuǎn)換為表格。


import math

from PySide import QtCore, QtGui



class Table2ListProxyModel(QtGui.QSortFilterProxyModel):

    def columnCount(self, parent=QtCore.QModelIndex()):

        return 1


    def rowCount(self, parent=QtCore.QModelIndex()):

        if parent.isValid():

            return 0

        return self.sourceModel().rowCount() * self.sourceModel().columnCount()


    def mapFromSource(self, sourceIndex):

        if (

            sourceIndex.isValid()

            and sourceIndex.column() == 0

            and sourceIndex.row() < self.rowCount()

        ):

            r = sourceIndex.row()

            c = sourceIndex.column()

            row = c * sourceIndex.model().columnCount() + r

            return self.index(row, 0)

        return QtCore.QModelIndex()


    def mapToSource(self, proxyIndex):

        r = proxyIndex.row() / self.sourceModel().columnCount()

        c = proxyIndex.row() % self.sourceModel().columnCount()

        return self.sourceModel().index(r, c)


    def index(self, row, column, parent=QtCore.QModelIndex()):

        return self.createIndex(row, column)



class List2TableProxyModel(QtGui.QSortFilterProxyModel):

    def __init__(self, columns=1, parent=None):

        super(List2TableProxyModel, self).__init__(parent)

        self._columns = columns


    def columnCount(self, parent=QtCore.QModelIndex()):

        r = self.sourceModel().rowCount()

        if r < self._columns:

            return r

        return self._columns


    def rowCount(self, parent=QtCore.QModelIndex()):

        if parent.isValid():

            return 0

        row = math.ceil(self.sourceModel().rowCount()*1.0 / self._columns)

        return row


    def index(self, row, column, parent=QtCore.QModelIndex()):

        return self.createIndex(row, column)


    def data(self, index, role=QtCore.Qt.DisplayRole):

        r = index.row()

        c = index.column()

        row = r * self.columnCount() + c

        if row < self.sourceModel().rowCount():

            return super(List2TableProxyModel, self).data(index, role)


    def mapFromSource(self, sourceIndex):

        r = math.ceil(sourceIndex.row()*1.0 / self.columnCount())

        c = sourceIndex.row() % self.columnCount()

        return self.index(r, c)


    def mapToSource(self, proxyIndex):

        if proxyIndex.isValid():

            r = proxyIndex.row()

            c = proxyIndex.column()

            row = r * self.columnCount() + c

            return self.sourceModel().index(row, 0)

        return QtCore.QModelIndex()


    def setSourceModel(self, model):

        model.rowsRemoved.connect(self.reset_model)

        model.rowsInserted.connect(self.reset_model)

        model.dataChanged.connect(self.reset_model)

        model.rowsMoved.connect(self.reset_model)

        model.layoutChanged.connect(self.reset_model)

        model.modelReset.connect(self.reset_model)

        super(List2TableProxyModel, self).setSourceModel(model)


    @QtCore.Slot()

    def reset_model(self):

        QtCore.QTimer.singleShot(0, self.invalidate)


    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):

        if role == QtCore.Qt.DisplayRole:

            return str(section)

        return super(List2TableProxyModel, self).headerData(

            section, orientation, role

        )



class CustomTableView(QtGui.QTableView):

    def __init__(self, parent=None):

        super(CustomTableView, self).__init__(parent)

        custom_model = QtGui.QStandardItemModel()

        datas = (("ad", "cd", "ef"), ("ab", "ce", "eg"), ("aa", "cb", "eh"))

        for i, data in enumerate(datas):

            for v, text in enumerate(data):

                custom_model.setItem(i, v, QtGui.QStandardItem(text))

        self.proxy_list = Table2ListProxyModel(self)

        self.proxy_list.setSourceModel(custom_model)

        self.proxy_sort_filter = QtGui.QSortFilterProxyModel(self)

        self.proxy_sort_filter.setSourceModel(self.proxy_list)

        self.proxy_table = List2TableProxyModel(

            columns=custom_model.columnCount(), parent=self

        )

        self.proxy_table.setSourceModel(self.proxy_sort_filter)


        self.setModel(self.proxy_table)


    @QtCore.Slot(str)

    def setFilter(self, text):

        self.proxy_sort_filter.setFilterWildcard(

            "*{}*".format(text) if text else ""

        )

        self.proxy_sort_filter.sort(0 if text else -1, QtCore.Qt.AscendingOrder)



class Window(QtGui.QWidget):

    def __init__(self):

        super(Window, self).__init__()


        self.line_edit = QtGui.QLineEdit()

        self.tableview = CustomTableView()


        lay = QtGui.QVBoxLayout(self)

        lay.addWidget(self.line_edit)

        lay.addWidget(self.tableview)

        self.line_edit.textChanged.connect(self.tableview.setFilter)



if __name__ == "__main__":

    import sys


    app = QtGui.QApplication(sys.argv)

    w = Window()

    w.show()

    sys.exit(app.exec_())

http://img1.sycdn.imooc.com//61e6ba660001e9fb03430275.jpghttp://img1.sycdn.imooc.com//61e6ba710001901d03420274.jpghttp://img1.sycdn.imooc.com//61e6ba79000109a503450273.jpg

查看完整回答
反對 回復(fù) 2022-01-18
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號