How to show the QListWidget selected item in QLineEdit ? . Here is My code. In the second File line, no 59 to 61 have a problem. problem: Attribute Error, None type . How to rectify?
In My first file (example_main_file.py) , I have a QLineEdit and QListWidget with items. My second file (example_source_file), have a methods to process/filter the QListwidget items from QLineEdit and shows the QListwidget selected item. But the result is not fruitful .
First File / example_main_file.py
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget,QListWidget
from example_source_file import Sourcefile
item = ["Python", "Python 2.7", "Python 2.9", "Python 3.5", "Python 3.7", "National", "Zebra",
"Apple", "X Ray", "Boat", "Tiger", "Item001", "Item002", "Item003", "Item004", "Item005",
"001Item", "002Item", "003Item", "004Item", "005Item", "Ball", "Cat", "Dog", "Fish",
"Gold Fish", "Star Fish", "2821", "2822", "2823", "2811", "2812", "2813"]
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox = QLineEdit(self)
self.textbox.setGeometry(100, 100, 300, 30)
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(100,150,300,30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self)
self.lbox2.setGeometry(100,200,300,500)
self.lbox1.addItems(item)
self.lbox2.addItems(item)
process = Sourcefile(self.textbox, self.lbox1, self.lbox2)
process1 = Sourcefile(self.textbox1, self.lbox1, self.lbox2)
self.textbox.textChanged.connect(process.func_textbox_textchanged)
self.textbox1.textChanged.connect(process1.func_textbox_textchanged)
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
Second File / example_source_file.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
flag = 1
search_text_length = 0
startitem_rowno = None
enditem_rowno = None
class Sourcefile(QObject):
def __init__(self, textbox,listbox1,listbox2,parent=None):
super().__init__(listbox2)
global startitem_rowno,enditem_rowno
self.tbox1 = textbox
self.lbox1 = listbox1
self.lbox2 = listbox2
self.tbox1.installEventFilter(self)
self.lbox2.installEventFilter(self)
startitem_rowno = 0
enditem_rowno = len(self.lbox2) - 1
def eventFilter(self, source, event):
global cursor_position, textbox_value
if event.type() == QEvent.KeyPress and source is self.tbox1:
if event.modifiers() == Qt.AltModifier and event.key() == Qt.Key_S:
self.func_item_startswith()
return True
if event.key() == Qt.Key_Down:
self.lbox2.setFocus()
self.lbox2.setCurrentRow(startitem_rowno)
cursor_position = self.tbox1.cursorPosition()
textbox_value = self.tbox1.text()
if event.key() == Qt.Key_Up:
self.lbox2.setFocus()
self.lbox2.setCurrentRow(enditem_rowno)
cursor_position = self.tbox1.cursorPosition()
textbox_value = self.tbox1.text()
if event.key() == Qt.Key_Return:
if len(self.lbox2) == 1:
self.lbox2.setCurrentRow(0)
prin = self.lbox2.currentItem().text()
print(prin)
if event.type() == QEvent.KeyPress and source is self.lbox2:
if event.key() == Qt.Key_Left or event.key() == Qt.Key_Backspace:
self.lbox2.clearFocus()
self.tbox1.setFocus()
elif event.key() == Qt.Key_Return:
prin = self.lbox2.currentItem().text()
print(prin)
self.tbox1.setText(prin)
#self.label_selected_item.setText("Selected Item : " + prin)
#self.label_selected_item.adjustSize()
self.tbox1.setFocus()
self.tbox1.setText(prin)
elif event.key() == Qt.Key_Up or event.key() == Qt.Key_Down:
pass
return super(Sourcefile, self).eventFilter(source, event)
def func_item_startswith(self):
global flag, startitem_rowno, enditem_rowno
flag = 1
startitem_rowno = 0
enditem_rowno = startitem_count - 1
self.lbox2.clear()
if startitem_count > 0:
for item in item_startswith:
self.lbox2.addItem(item.text())
else:
print("No Matching from start item")
def func_item_normal(self):
global falg,startitem_rowno,enditem_rowno
flag = 0
startitem_rowno = 0
enditem_rowno = normal_count - 1
self.lbox2.clear()
if normal_count > 0:
for item in item_normal:
self.lbox2.addItem(item.text())
def func_textbox_textchanged(self, txt):
global search_text, search_text_length, total_listbox_item, availableitem_count, normal_count, startitem_count, \
containitem_count, enditem_count, item_normal, item_startswith, item_contains, item_endswith, flag, \
startitem_rowno, enditem_rowno
search_text = self.tbox1.text()
search_text_length = len(search_text)
total_listbox_item = len(self.lbox2)
item_normal = self.lbox1.findItems("*", Qt.MatchWildcard)
item_startswith = self.lbox1.findItems(search_text, Qt.MatchStartsWith)
normal_count = len(item_normal)
startitem_count = len(item_startswith)
self.func_item_normal()
if search_text_length >= 1: pass
else: flag = 1
self.lbox2.clear()
if flag == 1:
self.func_item_startswith()
else:
self.func_item_normal()
def listbox_clicked(self, item):
self.tbox1.setText(item.text())
self.tbox1.setFocus()
Error / Result
Traceback (most recent call last):
File "D:\PyQt5_samples\example_source_file.py", line 59, in eventFilter
prin = self.lbox2.currentItem().text()
AttributeError: 'NoneType' object has no attribute 'text'
When you press enter then the item is selected, it is copied to the text of the QLineEdit, and then the item filter is invoked which removes the item so there is no longer currentItem in the next one so it returns the error.
Given this there are 2 possible solutions:
elif event.key() == Qt.Key_Return:
item = self.lbox2.currentItem()
self.tbox1.setText(item.text())
self.tbox1.setFocus()
return True
elif event.key() == Qt.Key_Return:
item = self.lbox2.currentItem()
if item is not None:
self.tbox1.setText(item.text())
self.tbox1.setFocus()