python - PYQT Accessing and changing dynamically added controls -


i beginner gui's , pyqt. trying dynamically set grid of qcombobox's , qlineedit's. qcombobox can select choice , choice, fill in corresponding qlineedit numbers. problem i'm having creating link between first qcombobox , first qlineedit box. make function each row know better way. post sample code. thank or advice might have.

import sys pyqt5.qtcore import qcoreapplication pyqt5.qtgui import * pyqt5.qtwidgets import *  class window(qmainwindow):      def __init__(self):         super(window, self).__init__()         self.setgeometry(50, 50, 700, 600)          self.home()      def home(self):         test1choices = ['test1:','choice1', 'choice2', 'choice3', 'choice4','choice5', 'choice6', 'choice7', 'choice8', 'choice9']         test2choices= ['test2:','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']         in range(0,10):             choice1combobox = qcombobox(self)             choice1combobox.additems(test1choices)             choice1combobox.resize(150,25)             choice1combobox.move(30,(150+(i*35)))             choice1combobox.setcurrentindex(2)              choice2combobox = qcombobox(self)             choice2combobox.setobjectname("choice2combobox"+str(i))             choice2combobox.additems(test2choices)             choice2combobox.resize(75,25)             choice2combobox.move(200,(150+(i*35)))             choice2combobox.setcurrentindex(2)             choice2combobox.activated[str].connect(self.dosomething)              numtextbox = qlineedit(self)             numtextbox.setobjectname("numbox"+str(i))             numtextbox.move(325,(150+(i*35)))             numtextbox.resize(35,25)              result1textbox = qlineedit(self)             result1textbox.setobjectname("result1box"+str(i))             result1textbox.move(400,(150+(i*35)))             result1textbox.resize(100,25)             result1textbox.setenabled(0)              result2textbox = qlineedit(self)             result2textbox.setobjectname("result2box"+str(i))             result2textbox.move(525,(150+(i*35)))             result2textbox.resize(100,25)             result2textbox.setenabled(0)          self.show()      def dosomething(self):         numbers=['result1','result2','result3','result4','result5','result6','result7','result8','result9','result10','result11','result12','result13','result14','result15']  def run():     app = qapplication(sys.argv)     gui = window()     sys.exit(app.exec_())   run() 

to summarize bring in index of selected qcombobox. use index number reference answer in "numbers" array. print result in qlineedit in same row

we use sender() object emits signal, name of object setobjectname(), , search index, other objects findchildren(), example output union of selected texts.

add name choice1combobox:

choice1combobox.setobjectname("choice1combobox"+str(i)) 

dosomething function:

def dosomething(self, _):     sender = self.sender()      l = sender.objectname().split("choice1combobox")     if len(l) > 1:         number = l[1]     else:         number = sender.objectname().split("choice2combobox")[1]      combo1 = self.findchildren(qcombobox, "choice1combobox"+number)[0]     combo2 = self.findchildren(qcombobox, "choice2combobox"+number)[0]     obj = self.findchildren(qlineedit, "numbox"+number)[0]     obj.settext(combo1.currenttext() + " " +  combo2.currenttext()) 

complete code:

import sys pyqt5.qtcore import qcoreapplication pyqt5.qtgui import * pyqt5.qtwidgets import *  class window(qmainwindow):      def __init__(self):         super(window, self).__init__()         self.setgeometry(50, 50, 700, 600)          self.home()      def home(self):         test1choices = ['test1:','choice1', 'choice2', 'choice3', 'choice4','choice5', 'choice6', 'choice7', 'choice8', 'choice9']         test2choices= ['test2:','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']         in range(0,10):             choice1combobox = qcombobox(self)             choice1combobox.setobjectname("choice1combobox"+str(i))             choice1combobox.additems(test1choices)             choice1combobox.resize(150,25)             choice1combobox.move(30,(150+(i*35)))             choice1combobox.setcurrentindex(2)             choice1combobox.activated[str].connect(self.dosomething)              choice2combobox = qcombobox(self)             choice2combobox.setobjectname("choice2combobox"+str(i))             choice2combobox.additems(test2choices)             choice2combobox.resize(75,25)             choice2combobox.move(200,(150+(i*35)))             choice2combobox.setcurrentindex(2)             choice2combobox.activated[str].connect(self.dosomething)              numtextbox = qlineedit(self)             numtextbox.setobjectname("numbox"+str(i))             numtextbox.move(325,(150+(i*35)))             numtextbox.resize(35,25)              result1textbox = qlineedit(self)             result1textbox.setobjectname("result1box"+str(i))             result1textbox.move(400,(150+(i*35)))             result1textbox.resize(100,25)             result1textbox.setenabled(0)              result2textbox = qlineedit(self)             result2textbox.setobjectname("result2box"+str(i))             result2textbox.move(525,(150+(i*35)))             result2textbox.resize(100,25)             result2textbox.setenabled(0)          self.show()      def dosomething(self, _):         sender = self.sender()          l = sender.objectname().split("choice1combobox")         if len(l) > 1:             number = l[1]         else:             number = sender.objectname().split("choice2combobox")[1]          combo1 = self.findchildren(qcombobox, "choice1combobox"+number)[0]         combo2 = self.findchildren(qcombobox, "choice2combobox"+number)[0]         obj = self.findchildren(qlineedit, "numbox"+number)[0]         obj.settext(combo1.currenttext() +  " " + combo2.currenttext())     def run():     app = qapplication(sys.argv)     gui = window()     sys.exit(app.exec_())   run() 

Comments