简体   繁体   English

如何将QItemDelegate(QLineEdit)更改连接到QSortFilterProxyModel PyQt4?

[英]How to connect QItemDelegate (QLineEdit) change to QSortFilterProxyModel PyQt4?

I tried to set a QlineEdit by subclassing QItemDelegate in first row of my QtableView: 我试图通过在QtableView的第一行中将QItemDelegate子类化来设置QlineEdit:

class ExampleDelegate(QItemDelegate):
    def createEditor(self, parent, option, index):
        self.line_edit = QLineEdit(parent)
        return self.line_edit

class example(QDialog):
    def __init__(self):
        super(druglist, self).__init__()
        self.UI()
    def UI(self):
        self.table_view=QTableView()
        self.delegate = ExampleDelegate()   
        self.table_view.setItemDelegateForColumn(0, self.delegate)
        self.table_model=QStandardItemModel()

        self.table_proxy=QSortFilterProxyModel()
        self.table_proxy.setSourceModel(self.table_model)

        self.table_view.setModel(self.table_proxy)

        self.delegate.textChanged.connect(self.lineedit_textchange) //do something like this

     def lineedit_textchange(self,text):
        search=QRegExp(text,Qt.CaseInsensitive,QRegExp.RegExp)
        self.table_proxy_model.setFilterRegExp(search)

I just want to know how could I connect my ExampleDelegate text change to my lineedit_textchange function in main class? 我只想知道如何将我的ExampleDelegate文本更改连接到主类中的lineedit_textchange函数?

You should not connect to the line_edit in the delegate. 您不应该连接到委托中的line_edit The delegate is here to set a custom editor in multiple cells of your QTableView (here all the cells in column 0). 委托是在QTableView多个单元格(此处是第0列中的所有单元格)中设置自定义编辑器的步骤。 If you could connect to the line_edit in the delegate, how would you know which cell has been changed ? 如果您可以连接到委托中的line_edit ,您将如何知道哪个单元已更改?

What you want is to know when a cell in the column 0 has been changed. 您想知道何时更改了列0中的单元格。 Every model has the dataChanged signal: 每个模型都有dataChanged信号:

void QAbstractItemModel::dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight) 无效的QAbstractItemModel :: dataChanged(const QModelIndex&topLeft,const QModelIndex&bottomRight)

This signal is emitted whenever the data in an existing item changes. 只要现有项目中的数据发生更改,就会发出此信号。

You can connect to this signal to get the index of a cell that just changed. 您可以连接到该信号以获得刚刚更改的单元格的索引。 You can check whether this cell is in column 0, and proceed accordingly: 您可以检查此单元格是否在第0列中,然后进行相应的操作:

def UI(self):
    self.model=QStandardItemModel(4,2)
    self.model.dataChanged.connect(self.on_dataChanged)

    self.delegate=Delegate()

    self.view=QTableView()
    self.view.setItemDelegateForColumn(0,self.delegate)
    self.view.setModel(self.model)

def on_dataChanged(self,index,index2):
    print(index,index2)
    print(index.data())
    print(index.column())
    if index==index2 and index.column()==0:
        #do stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM