Alright, let us go back to basics. Forget QLineEdit, forget Qt. I can restate your problem abstractly as "I need to construct n objects of the same type, and uniquely associate each of them with an integer index in the range 0..n - 1; then, later, someone gives me an index value i in 0..n - 1, that I do not know at compile time, and I need to retrieve the object associated with i."
How would I do that in Python? Python 101: with a list (which is less optimized than an array in this case, but we don't care). Here is a minimal program that illustrates this. It prompts the user to type the value of n, then n integer values, then the value of i. It then prints the ith (counting from 0) integer:
values = []
n = int(input("Please enter the number of integers: "))
for idx in range(0, n):
val = int(input("Please enter the value of the next integer: "))
values.append(val)
i = int(input("Please enter the index of the integer you want me to print: "))
print(values[i])
values = []
n = int(input("Please enter the number of integers: "))
for idx in range(0, n):
val = int(input("Please enter the value of the next integer: "))
values.append(val)
i = int(input("Please enter the index of the integer you want me to print: "))
print(values[i])
To copy to clipboard, switch view to plain text mode
That's it. Every basic programming course on data structures must contain a variation of this.
Notice that I use a single variable, val, in the loop that populates the list, to hold the value of each element in turn. I have to do that, that is the whole point of a loop: I write a piece of code that will be executed an unbounded number of times, and I have finally many variables to store the state of each iteration; so, these variables get rewritten over and over. Is that a problem? No, because I use another data structure, the list values, to remember all the successive values of val, along with their index. Later on, I can find the ith element with values[i].
The point is: I do not rely on Python's environment (the mapping of variable names to values) to store all the integers in, say, variables named val0, val1, etc, added on the fly; I explicitly use a data structure for that. The environment is not meant to be used as a data structure.
Now, replace "integer" with "QLineEdit" and you end up with the problem you are trying to solve. So, why don't you do like any first year programming student and add a list/array/whatever to your program, so that we may all move on to real problems?
Bookmarks