Yes, because Designer has to pick an identifier for the field name in the code it generates. It could choose anything; it just makes sense to choose what the user specified in the objectName property.
However, these are two very different things.
The identifier of the field, like all identifiers in C++, is only present at compile time. E.g. when you write code such as
the compiler statically knows exactly where to find the pointer myTextEdit, what its type is, etc, and produces machine code that loads the value in this pointer and calls the appropriate method. myTextEdit is an identifier in the source code that does not appear in the executable (unless maybe you compile with debugging support). You could rename this identifier and recompile, and you would probably get the same binary.Qt Code:
myTextEdit->setText("Hello");To copy to clipboard, switch view to plain text mode
In particular, you cannot, at runtime, build a string "myTextEdit" and magically expect the C++ runtime to figure out that there was an identifier with this name in the source, retrieve its location and type, and allow you to use the object. Other languages support this kind of reflexion, but C++ does not. C++ requires you to do things explicitly. OTOH, Python may let you look up a field by its name at runtime.
Qt's meta-object system, among other things, attempts to add a form of reflexion. The objectName property, for instance, attaches a string to each QObject, that you can retrieve at runtime. You can therefore look for a QObject with a specific object name among a set of QObjects. However, you have to explicitly perform this search by calling a function implementing it. This is what the method QObject::findChild() does.
So, you need a lookup mechanism to retrieve a QLineEdit instance based on an index at runtime. What we have been trying to explain since the beginning of this thread is that, in your particular case, instead of using a costly generic mechanism like QObject::findChild() or Python's reflexion, you can simply store the QLineEdits in an array, and retrieve the element at the specified index. In fact, your problem is a textbook example of a situation in which you will naturally use an array. Every serious programming language has arrays, including Python, precisely for situations like this one.
Qt Code:
self.swInventory.setObjectName("swInventory") self.page.setObjectName("page") self.swInventory.addWidget(self.page) self.page_2.setObjectName("page_2") self.swInventory.addWidget(self.page_2)To copy to clipboard, switch view to plain text mode
This is code written by the designer for a stackedwidget. How can I write code like this that will loop and create a variable number of pages if I can't create the identifier names on the fly? The objectName is a string, so that is not a problem. Can I just use the same identifier (self.Page) with a different objectName for each page?
If you want to store the reference to each page then this has been answered at least a dozend times in this thread by now.
You can reuse a variable as often as you like. Everytime you assign an object reference to it, you can access that object's properties, including setting a different object name.
Obviously that doesn't make the object accessible by that name, but yeye_olive already explained the difference between an identifier and an object specific value.
Cheers,
_
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:
That's it. Every basic programming course on data structures must contain a variation of this.Qt Code:
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
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