Issue
I have a tablewidget with 1000-ish rows of data. And there's this nice feature auto-implemented for table that searches for the matched data whenever we press letter keys. I want to make it a little bit more intuitive with a visual aid.
I want to create a label that would display letters that the user has entered in the last 2 seconds. It should continue concatenating and displaying new letters. If the user stops entering new letters for more than 2 seconds then the label should disappear.
I think I need to create a sort of a "manager" function that would receive keypresses and create this label and terminates it when 2 seconds have passed. Problem is I don't know how to make this function "waits" for new keystrokes.
I suppose this visual aid is not something new and somebody probably implemented it before but I don't know what it's called to Google so I'm sorry for creating a post for it.
Solution
You can reimplement the keyboardSearch()
function of the tablewidget in a subclass, and then create a child widget that will update itself based on the search.
Note that the search is automatically reset based on the QApplication.keyboardInputInterval
, which makes appear the search widget for a very short time (until the view can accept a new search as part of the current one).
from PyQt5 import QtCore, QtWidgets
from random import choice
from string import ascii_lowercase
class TableSearch(QtWidgets.QTableWidget):
def __init__(self):
super().__init__(0, 3)
self.setEditTriggers(self.NoEditTriggers)
for row in range(500):
self.insertRow(row)
for col in range(3):
text = ''.join(choice(ascii_lowercase) for i in range(5))
self.setItem(row, col, QtWidgets.QTableWidgetItem(text))
self.searchWidget = QtWidgets.QLabel(self)
self.searchWidget.setStyleSheet('''
QLabel {
border: 1px inset darkGray;
border-radius: 2px;
background: palette(window);
}
''')
self.searchWidget.hide()
self.searchTimer = QtCore.QTimer(
singleShot=True,
timeout=self.resetSearch,
interval=QtWidgets.QApplication.instance().keyboardInputInterval())
def resetSearch(self):
self.searchWidget.setText('')
self.searchWidget.hide()
def updateSearchWidget(self):
if not self.searchWidget.text():
self.searchWidget.hide()
return
self.searchWidget.show()
self.searchWidget.adjustSize()
geo = self.searchWidget.geometry()
geo.moveBottomRight(
self.viewport().geometry().bottomRight() - QtCore.QPoint(2, 2))
self.searchWidget.setGeometry(geo)
def keyboardSearch(self, search):
super().keyboardSearch(search)
if not search:
self.searchWidget.setText('')
else:
text = self.searchWidget.text()
if not text:
text = 'Searching: '
text += search
self.searchWidget.setText(text)
self.updateSearchWidget()
self.searchTimer.start()
def resizeEvent(self, event):
super().resizeEvent(event)
self.updateSearchWidget()
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.