Reputation: 653
Want to change focus from the first textbox to next one,(change focus to next child or previous child). when I Press Enter/Return Key ( as like tab Key) . Attach my code. (First File: example_main_file.py) and the second one is ( example_source_file.py) . How to resolve?
First File - ( example_main_file.py)
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from example_source_file import Sourcefile
item1 = ["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"]
item2 = ["India","America","Russia","China","England","Iran","Iraq","Ice Land"]
item3 = ["FootBall","Volly Ball","Basket Ball","Ball","Hand Ball", "Rugby","Cricket","Long
Jump","High Jump"]
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox1 = QLineEdit(self)
self.textbox1.setGeometry(100, 100, 300, 30)
self.textbox2 = QLineEdit(self)
self.textbox2.setGeometry(100, 150, 300, 30)
self.textbox3 = QLineEdit(self)
self.textbox3.setGeometry(100, 200, 300, 30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self)
self.lbox2.setGeometry(100,250,300,500)
self.textbox1.setObjectName("textbox1")
self.textbox2.setObjectName("textbox2")
self.textbox3.setObjectName("textbox3")
self.lbox2.setObjectName("listbox2")
self.updateallwidgets()
QApplication.instance().focusChanged.connect(self.on_focusChanged)
def updateallwidgets(self):
QWidget.allwidgets = QApplication.allWidgets()
for i in QWidget.allwidgets:
j = i.objectName()
def on_focusChanged(self):
fwidget = QApplication.focusWidget()
if fwidget is not None:
if fwidget.objectName() == "textbox1":
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item1)
self.lbox2.addItems(item1)
self.getdetails_1 = Sourcefile(self.textbox1,self.lbox1,self.lbox2)
self.textbox1.textChanged.connect(self.getdetails_1.func_textbox_textchanged)
elif fwidget.objectName() == "textbox2":
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item2)
self.lbox2.addItems(item2)
self.getdetails_2 = Sourcefile(self.textbox2,self.lbox1,self.lbox2)
self.textbox2.textChanged.connect(self.getdetails_2.func_textbox_textchanged)
elif fwidget.objectName() == "textbox3":
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item3)
self.lbox2.addItems(item3)
self.getdetails_3 = Sourcefile(self.textbox3,self.lbox1,self.lbox2)
self.textbox3.textChanged.connect(self.getdetails_3.func_textbox_textchanged)
return True
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
selected_item = None
class Sourcefile(QWidget):
def __init__(self, textbox, listbox1,listbox2,parent=None):
super().__init__(listbox2)
global startitem_rowno,enditem_rowno
self.setFocusPolicy(Qt.StrongFocus)
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,selected_item
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()
print(cursor_position)
print(textbox_value)
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:
print("return key pressed")
#self.parent().focusNextChild()
#self.focusNextPrevChild(True)
############# I am Struk here ##############
if event.type() == QEvent.KeyPress and source is self.lbox2:
if event.key() == Qt.Key_Left or event.key() == Qt.Key_Backspace:
self.tbox1.setFocus()
return True
elif event.key() == Qt.Key_Return:
self.selected_item = self.lbox2.currentItem().text()
if self.selected_item is not None:
self.tbox1.setText(self.selected_item)
self.tbox1.setFocus()
return True
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()
Upvotes: 1
Views: 959
Reputation: 5805
You can connect the returnPressed
signal of your QLineEdits to set the focus of the next widget, e.g.
self.textbox1.returnPressed.connect(lambda: self.textbox2.setFocus(Qt.ShortcutFocusReason))
Also, you can set the objectName
of your widgets with a keyword argument and save a line of code, e.g.
self.textbox1 = QLineEdit(self, objectName = 'textbox1')
I only worked with your example_main_file.py. I think this gives the behahavior you want:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
# ~ from example_source_file import Sourcefile
item1 = ["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"]
item2 = ["India","America","Russia","China","England","Iran","Iraq","Ice Land"]
item3 = ["FootBall","Volly Ball","Basket Ball","Ball","Hand Ball",
"Rugby","Cricket", "Long Jump","High Jump"]
class Check(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Check Window")
self.textbox1 = QLineEdit(self, objectName = 'textbox1')
self.textbox1.setGeometry(100, 100, 300, 30)
self.textbox2 = QLineEdit(self, objectName = 'textbox2')
self.textbox2.setGeometry(100, 150, 300, 30)
self.textbox3 = QLineEdit(self, objectName = 'textbox3')
self.textbox3.setGeometry(100, 200, 300, 30)
self.lbox1 = QListWidget()
self.lbox2 = QListWidget(self, objectName = 'listbox2')
self.lbox2.setGeometry(100,250,300,500)
self.textbox1.returnPressed.connect(lambda: self.textbox2.setFocus(Qt.ShortcutFocusReason))
self.textbox2.returnPressed.connect(lambda: self.textbox3.setFocus(Qt.ShortcutFocusReason))
self.textbox3.returnPressed.connect(lambda: self.lbox2.setFocus(Qt.ShortcutFocusReason))
self.updateallwidgets()
QApplication.instance().focusChanged.connect(self.on_focusChanged)
def updateallwidgets(self):
QWidget.allwidgets = QApplication.allWidgets()
for i in QWidget.allwidgets:
j = i.objectName()
def on_focusChanged(self):
fwidget = QApplication.focusWidget()
if fwidget is not None:
if fwidget.objectName() == "textbox1":
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item1)
self.lbox2.addItems(item1)
# ~ self.getdetails_1 = Sourcefile(self.textbox1,self.lbox1,self.lbox2)
# ~ self.textbox1.textChanged.connect(self.getdetails_1.func_textbox_textchanged)
elif fwidget.objectName() == "textbox2":
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item2)
self.lbox2.addItems(item2)
# ~ self.getdetails_2 = Sourcefile(self.textbox2,self.lbox1,self.lbox2)
# ~ self.textbox2.textChanged.connect(self.getdetails_2.func_textbox_textchanged)
elif fwidget.objectName() == "textbox3":
self.lbox1.clear()
self.lbox2.clear()
self.lbox1.addItems(item3)
self.lbox2.addItems(item3)
# ~ self.getdetails_3 = Sourcefile(self.textbox3,self.lbox1,self.lbox2)
# ~ self.textbox3.textChanged.connect(self.getdetails_3.func_textbox_textchanged)
return True
def main():
myapp = QApplication(sys.argv)
mywin = Check()
mywin.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
Upvotes: 3