I tried to set a QlineEdit by subclassing QItemDelegate in first row of my QtableView:
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?
You should not connect to the line_edit
in the delegate. The delegate is here to set a custom editor in multiple cells of your QTableView
(here all the cells in column 0). If you could connect to the line_edit
in the delegate, how would you know which cell has been changed ?
What you want is to know when a cell in the column 0 has been changed. Every model has the dataChanged
signal:
void 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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.