PDA

View Full Version : subclass QLineEdit to have an index



nlgootee
1st May 2016, 00:07
I need to subclass a PyQt5 QLineEdit so that it has an index, such that I could access it by:

stringvariable = LineEdit(0).text()
stringvariable = LineEdit(1).text()
stringvariable = LineEdit(2).text()

Is this possible?

anda_skoa
1st May 2016, 09:18
So is this the same LineEdit object? What would the index do?
Or are these three LineEdit objects?

Cheers,
_

nlgootee
1st May 2016, 15:35
Sorry if my code was confusing. I need a subclassed LineEdit with an index attribute that I can set when the object is created. I would like to access it with LineEdit(index).text()

Is this possible?

anda_skoa
1st May 2016, 21:36
I still don't understand what that "index" attribute is for.
It sounds like you want to have a list of line edits.

Lets try this visually: do you have one line edit or more than one?

Cheers,
_

nlgootee
3rd May 2016, 16:38
It is for a column of LineEdits. I need to subclass one (with an index) and then make lots of copies with individual index numbers.

anda_skoa
3rd May 2016, 17:29
What does the class do with the index number?

Cheers,
_

d_stranz
3rd May 2016, 21:09
It is for a column of LineEdits. I need to subclass one (with an index) and then make lots of copies with individual index numbers.

Sounds to me like you are confusing how your program keeps track of multiple QLineEdit instances with the QLineEdit instances themselves. If you simply stick the QLineEdit pointers into a vector as you create them, then the index in the vector -is- the index of the QLineEdit. If all of your QLineEdit instances are connected to the same slot, then you can use the QObject::sender() method in the slot to get the pointer to the specific QLineEdit instance that sent the signal, and then search the vector to get the index.

Or if your "index" is non-sequential, you can make a QMap< QWidget *, int > to map the QLineEdit pointer to any integer.

Or you can use QSignalMapper to associate each QLineEdit instance with an integer.

There are lots of ways to do what I think you are trying to do without having to make a custom widget.

nlgootee
3rd May 2016, 22:50
What does the class do with the index number?

Cheers,
_

The class doesn't do anything with the index. I have a routine that creates a grid consisting of rows with a textbox followed by some labels. They are created in code using a series of for loops. If I can create a subclassed LineEdit(index) I will be able to use an existing routine written in VB6.

Added after 6 minutes:


Sounds to me like you are confusing how your program keeps track of multiple QLineEdit instances with the QLineEdit instances themselves. If you simply stick the QLineEdit pointers into a vector as you create them, then the index in the vector -is- the index of the QLineEdit. If all of your QLineEdit instances are connected to the same slot, then you can use the QObject::sender() method in the slot to get the pointer to the specific QLineEdit instance that sent the signal, and then search the vector to get the index.

Or if your "index" is non-sequential, you can make a QMap< QWidget *, int > to map the QLineEdit pointer to any integer.

Or you can use QSignalMapper to associate each QLineEdit instance with an integer.

There are lots of ways to do what I think you are trying to do without having to make a custom widget.

Thanks for the reply. I don't understand what you said so it doesn't help much. What is a vector? I appreciate the different methods to do what you think that I want to do, but what I really want is to know if it is possible to subclass a QLineEdit with an index and access it in this format: LineEdit(index) not LineEdit.index

anda_skoa
4th May 2016, 09:19
The class doesn't do anything with the index.

Then why does it need the index?



I have a routine that creates a grid consisting of rows with a textbox followed by some labels. They are created in code using a series of for loops.

Ok, that is possible with normal QLineEdits as well.



If I can create a subclassed LineEdit(index) I will be able to use an existing routine written in VB6.

So what does the VB6 line edit object use the index for?
There must be a reason why the line edit object itself needs to know which index it stands for.



What is a vector?

A vector is a linear container, like a list or array.
Its elements can be accessed by index.



I appreciate the different methods to do what you think that I want to do, but what I really want is to know if it is possible to subclass a QLineEdit with an index

Of course, you can store arbitrary data in a class, so creating a subclass of QLineEdit that stores an integer is possible.



and access it in this format: LineEdit(index) not LineEdit.index
The first creates an object of class LineEdit and passes "index" to its constructor.
The second accesses an object called LineEdit and accesses its "index" property.

As d_stranz said and what I am trying to find out since several iterations is if you need the index inside the object or if you want to access several objects by index.

Because the code snippet suggest the latter but you keep insisting on the former.

Cheers,
_

nlgootee
4th May 2016, 13:19
Then why does it need the index?


Ok, that is possible with normal QLineEdits as well.


So what does the VB6 line edit object use the index for?
There must be a reason why the line edit object itself needs to know which index it stands for.


A vector is a linear container, like a list or array.
Its elements can be accessed by index.


Of course, you can store arbitrary data in a class, so creating a subclass of QLineEdit that stores an integer is possible.


The first creates an object of class LineEdit and passes "index" to its constructor.

_

That is what I need. I am creating a screen displaying 1650 inventory items. The lineedit is used to enter a quantity for an order. When a lineedit has a number entered, the index is used to access the information in the corresponding labels in that row (using the same index) and passing that to a page where the invoice is displayed and a routine that prints a hard copy. I want to create a LineEdit, assign an index and place it on a form in a loop that will create a grid with all 1650 items.

anda_skoa
4th May 2016, 13:32
For the access you put the line edit in a list, vector or array, whatever the preferred data structure for that is in Python.

If you really need to store the index value in the object as well, just call setProperty() on it, that will create a dynamic property with any name you choose.

Cheers,
_

d_stranz
4th May 2016, 17:28
I want to create a LineEdit, assign an index and place it on a form in a loop that will create a grid with all 1650 items.

Just so everyone is clear on what you are trying to do, are you creating one QLineEdit instance that can be used to edit the contents of any of the 1650 cells in your grid, or are you creating 1650 QLineEdit instances, one for each cell in the grid? Or something different?

And in your example, what is "LineEdit( index )" supposed to return? A single LineEdit instance? You can't accomplish that by assigning an "index" as a property of a QLineEdit. A QLineEdit instance is a single instance, it isn't a collection of multiple instances. If you have a red apple on your desk, how could you ask it to give you a yellow apple instead? That seems to be what you think you can do by giving the QLineEdit this magical "index".

You can accomplish that by putting your (multiple) QLineEdit instances into an array or vector and asking for "myArrayOfLineEdits( index )".

nlgootee
5th May 2016, 18:40
Just so everyone is clear on what you are trying to do, are you creating one QLineEdit instance that can be used to edit the contents of any of the 1650 cells in your grid, or are you creating 1650 QLineEdit instances, one for each cell in the grid? Or something different?".

Ok, I have a loop that creates a LineEdit and places it on a form. Then the loop creates a second LineEdit and places it on the form just below the first one. (loop 1650 times) Now I type something in the first LineEdit. I type something else in the second LineEdit. How do I tell them apart? They both have the same name because they were created in a loop. What is the difference between them? How do I get the text from one of them and know which one it is coming from? I use an index! The first one is named LineEdit(0) and the second one is named LineEdit(1). I get the text from the first one with string = LineEdit(0).text() and I get the text from the second one with string = LineEdit(1).text(). The index is to tell them apart from each other.

anda_skoa
6th May 2016, 10:44
You only need the index inside the object if you need the value inside the object or if you need to ask the object later what index it has.

Access of an object at a given index can easily be done with just storing the object in an index accessible container, e.g. a list or vector.
You can use QSignalMapper to map each line edit's editingFinished() signal to a slot call with the index.

Cheers,
_

d_stranz
6th May 2016, 15:03
What is the difference between them?

Every one of them is a unique QLineEdit instance: each one has a unique pointer value. If you are connecting all of them to the same slot that will be called when the line edit emits the editingFinished() signal, then you tell them apart based on their pointers. The pointer is accessible if you call the sender() method inside the slot. If you store these pointers in a list or vector as you create them and add them to your form, then you can look up this pointer value in this list and its position in the list is the index.

Or, as I (and anda_skoa also) have suggested, you can use QSignalMapper to directly map the QLineEdit's pointer value to any integer. You connect to the QSignalMapper::mapped() signal. In the slot that connects to this, you use the integer that is passed in as the argument to QSignalMapper::mapping() to get the QLineEdit instance that triggered the signal in the first place.

nlgootee
6th May 2016, 17:19
You guys really make it hard. I am going to subclass a LineEdit to have an index. The index for each LineEdit is going to be the unique part number for the item in that row. I have a perfectly workable routine that uses the index to locate each LineEdit and retrieve the data from that row. I would like to subclass the LineEdit so that I can access it using this format: string = LineEdit(index).text(). If I can't use that format, I will have to try something else but first I want to know if it is possible to subclass a LineEdit with an index attribute that I can set when the object is created and I would like to access it with LineEdit(index).text()

Added after 5 minutes:


You only need the index inside the object if you need the value inside the object or if you need to ask the object later what index it has.

Access of an object at a given index can easily be done with just storing the object in an index accessible container, e.g. a list or vector.
You can use QSignalMapper to map each line edit's editingFinished() signal to a slot call with the index.

Cheers,
_

I really don't understand what you are saying. I know that what I am trying to do is not that complicated, but what you are describing sounds very complicated and I don't see how it would do what I need it to do.

anda_skoa
6th May 2016, 17:41
I am going to subclass a LineEdit to have an index. The index for each LineEdit is going to be the unique part number for the item in that row.

Ok, that is easy, right?



I have a perfectly workable routine that uses the index to locate each LineEdit and retrieve the data from that row.

Excellent.



I would like to subclass the LineEdit so that I can access it using this format: string = LineEdit(index).text()

So you want to create an instance of your line edit and immediately get the text from it?
You know that this will always be an empty string, right?



If I can't use that format, I will have to try something else

Like storing each line edit object in a sequence container and using the index to access the correct object?



but first I want to know if it is possible to subclass a LineEdit with an index attribute

Of course that is possible.



that I can set when the object is created and I would like to access it with LineEdit(index).text()

Sure, but why would you want to access the text of a newly create object?
Wouldn't you rather have the text the users enter?

Cheers,
_

d_stranz
7th May 2016, 17:52
The index for each LineEdit is going to be the unique part number

What we have here is a failure to communicate. When we read the word "index", it translates to "location", meaning the position that something occupies in a list, array, matrix, whatever, and is something that can be used in software to pull the instance of the actual software object out of that collection.

You now have revealed that you are using the term "index" to mean "part number", which in my translation, could be a UPC code, an alphanumeric product code used by a company's inventory system, and which, in general, has no relationship to how or where an object used to edit that instance is stored in a collection.

You also seem to be confused about Python syntax. If "LineEdit" in Python is the name of a class derived from the class we refer to as "QLineEdit" in C++ (with the addition of an "index" data member and a constructor that can assign that index), then the statement:


LineEdit( index ).text()

does this:

1 - It creates a temporary instance of a new LineEdit object
2 - It assigns the value of the "index" argument to the "index" member variable
3 - It calls the base class (QLineEdit) text() method to ask for the string currently contained in the new instance.
4 - It destroys the temporary LineEdit instance at the end of the statement.
5 - If the return value of text() isn't assigned to anything, it goes away too.

Unless your LineEdit class constructor / initializer also initializes the string, or unless your LineEdit class overrides text() to return something other than an empty string, the line of code does absolutely nothing other than return an empty string from a transient object.

So if what you really want to do is something like "given a part number, return to me the text contained in the line edit instance referred to by the part number" (where you use the word "index" to mean "part number"), you can't do that with the code you are proposing. There is no way you can store a part number in a LineEdit instance and use code like LineEdit( partNumber ) to return to you a previously created LineEdit instance in which you have stored that part number.

As anda_skoa and I have been trying to get you to understand, you need some mechanism external to the LineEdit instances you create that allows you to store these instances and look them up by index, part number, whatever.

nlgootee
8th May 2016, 14:43
What we have here is a failure to communicate.

As anda_skoa and I have been trying to get you to understand, you need some mechanism external to the LineEdit instances you create that allows you to store these instances and look them up by index, part number, whatever.

No, I actually don't need to store them. I will try to explain more clearly how I want this to work. I create a column of 1650 rows that consist of a QLineEdit(call it MyLineEdit(item#)) and 2 or 3 Labels (item#, description, price) the lineedit and labels are indexed with the same number. The salesperson while making an order scrolls down the column and at each item in the order places a number(quantity) in the LineEdit. When the order is complete, the text of the LineEdit and the labels is placed in the invoice and saved in a database and the text is cleared from all of the LineEdits to be ready for the next order. The LineEdits exist on the screen until the application is closed, the text is only there until it is saved in the database. If in the future, someone needs to look at the order, a query is run using the OrderID and the order is recreated placing the quantity of each item in the order in the LineEdits using MyLineEdit(item#).setText(). The LineEdits are created at the beginning of the application and destroyed when it closes, I don't need to store them anywhere. When the order is created, I can use the textChanged signal to store the information and probably don't actually need the item# index, but when the order is recreated being able to use MyLineEdit(item#).setText() is much better than the way it was done in the original application. I hope that this helps to clear thing up. I really appreciate your help with this. Thanks

ars
8th May 2016, 16:40
Hello,

from your recent description of the use case I suggest using a QTableView with a suitable model for solving your task. The table view would show the item description, part number, ... and a field for entering the number of items in each row. The table itself will show 1650 rows.

Using a table view with 1650 rows and 3+ columns has several advantages compared to your idea:
1. It handles the layout of your display automatically based on size constraints (width and height) of its cells (your items). In your approach you have to do that yourself.
2. It automatically sets up scroll bars and the handling of them
3. It is a single widget to be placed in the GUI. With your approach you have to put 1650 times [ 2 QLabels (for item description and part number) + 1 QLineEdit (for number of ordered items)] = 4950 widgets into your GUI form. This is a waste of resources and creating such a big number of widgets and populating them with text will slow down your software.
4. Using a table for display + a model for storing the data is future proof and allows you to easily adopt to new requirements, e.g. adding more items to your product portfolio, adding additional information to items (translates to add new columns)
5. Your users surely won't be happy to search the item they want to order by scrolling to a specific line. They might require some sort of filtering to reduce the number of items displayed. Filtering is already supported by Qt models.

So have a look at http://doc.qt.io/qt-5/qtableview.html and its base class http://doc.qt.io/qt-5/qabstractitemview.html. Qt documentation provides examples on how to set them up.

Best regards
ars

ChrisW67
8th May 2016, 21:41
The OPs question makes more sense if you understand how Visual Basic 6 handled controls on a form. A form could have a number fully independent text boxes (equivalent of QLineEdit) each with an individual name as you would see in a Qt form. However, VB6 also had an upper limit on the number of controls on a form (256 IIRC). If you needed to exceed that then VB6 provided a method that could be used to provide a flyweight version of a control with single name and index to access many instances of that control type: a control array. That is, edit(1) referred to the first editor, edit(2) to the second, etc. but there was only one actual control object. Once the control gained an index its event handlers (like slots) were automagically passed an integer index number as their first argument.


The most direct translation of that is to a list of QLineEdits and a QSignalMapper (but the behaviours also bear striking resemblance to the transient editor in item views). I guess that in Python th "control array" might look some like this (only passing knowledge of Python):


self.TextBox = []
for i in range(0, 1500):
self.TextBox.append(new QLineEdit)
layout.addWidget(self.TextBox[i])

# and later
Str = TextBox[123].text()

Actually, the QSignalMapper docs have an almost complete example of the entire process including channelling signals from 1500 widgets through a single set of slots with an integer index.

d_stranz
9th May 2016, 00:05
The OPs question makes more sense if you understand how Visual Basic 6 handled controls on a form.

And the OP seems to want to force Qt and Python to use the same semantics, even though it isn't possible using the syntax he is proposing. As we have already suggested, the closest thing to that is to use QSignalMapper, but that doesn't appear to satisfy the need to adhere to VB syntax. My head hurts from pounding it against the wall, so I'll leave it to the rest of you to carry on.

nlgootee
9th May 2016, 15:29
I am quite familiar with tableViews and that is not an acceptable solution. My earlier description was considerably shortened and was meant only to demonstrate why I want to subclass a LineEdit(). As I have mentioned before, I don't really need a different way to do what I want, I need help doing what I do want to do, which is to subclass a LineEdit so that I can access it with LineEdit(item#).setText()

d_stranz
9th May 2016, 19:10
For the nth (and last for me) time, you cannot access an existing LineEdit, subclassed or not, using the syntax LineEdit( item#). The syntax you seem bent on using will create a new LineEdit instance (as I explained several posts ago). It will not give you a pointer to a LineEdit instance that you create using the same syntax and place on a form. If you think you can access a previously-created LineEdit this way, you are wrong. Python and C++ do not work the same way as VB and Excel (as ChrisW also explained). You may be able to use your syntax in a VB macro, but you cannot use the same syntax in a Python script.

The languages are different. They don't work the same way. What you can do in VB you can't do in Python or C++. And behind the scenes, VB is likely doing exactly the same thing as we have told you that you must do - it has created some type of lookup mechanism that stores these instances and allows the type of syntax you want to use. To do the same thing in Python, you must write your own lookup mechanism, and you cannot use VB syntax as part of that.

Sorry if this sounds rude, but your stubbornness in refusing to accept that you cannot do in Python what you might be able to do in VB is pretty frustrating. You have received advice from several people who regularly give their free time to try to help others who ask questions here, and we have tried to be patient in explaining why the syntax you want to use won't work and in offering you solutions which will work in Python and C++. You keep insisting that your way is the only way. Good luck with it.

yeye_olive
10th May 2016, 10:07
@nlgootee

You realize that, to the rest of the world, porting an algorithm from a language to another generally means preserving the semantics while adopting the syntax and best practices of the target language?

But hey, good news, C++ lets you define your own operators. So, technically, nothing stops you from writing your own insane vector class and get exactly the syntax you want. Behold the majesty of C++ which lets you have your way without listening to reason; I get goosebumps sometimes:

#include <cstddef>
#include <vector>

class MyStrangeVector {
public:
void add(QLineEdit *le) {
m_vec.push_back(le);
}

// Index using () and dereference, just because we can
QLineEdit &operator()(size_t index) {
return *m_vec[index];
}

private:
std::vector<QLineEdit *> m_vec;
};

void f() {
MyStrangeVector LineEdit;

// Populate the vector
for (size_t i = 0; i < 1650; ++i) {
QLineEdit *le = new QLineEdit(/* ... */);
// ...
LineEdit.add(le);
}

// All this nonsense so that we can finally write:
LineEdit(42).setText("meh");
}
Thanks. You've made my day.

nlgootee
13th May 2016, 20:57
The OPs question makes more sense if you understand how Visual Basic 6 handled controls on a form. A form could have a number fully independent text boxes (equivalent of QLineEdit) each with an individual name as you would see in a Qt form. However, VB6 also had an upper limit on the number of controls on a form (256 IIRC). If you needed to exceed that then VB6 provided a method that could be used to provide a flyweight version of a control with single name and index to access many instances of that control type: a control array. That is, edit(1) referred to the first editor, edit(2) to the second, etc. but there was only one actual control object. Once the control gained an index its event handlers (like slots) were automagically passed an integer index number as their first argument.


The most direct translation of that is to a list of QLineEdits and a QSignalMapper (but the behaviours also bear striking resemblance to the transient editor in item views). I guess that in Python th "control array" might look some like this (only passing knowledge of Python):


self.TextBox = []
for i in range(0, 1500):
self.TextBox.append(new QLineEdit)
layout.addWidget(self.TextBox[i])

# and later
Str = TextBox[123].text()

Actually, the QSignalMapper docs have an almost complete example of the entire process including channelling signals from 1500 widgets through a single set of slots with an integer index.

What does OP stand for? I got a very similar reply on a different list, using a dictionary. I will probably end up doing something like it but it seems like overkill for what I am trying to do.


@nlgootee

You realize that, to the rest of the world, porting an algorithm from a language to another generally means preserving the semantics while adopting the syntax and best practices of the target language?

But hey, good news, C++ lets you define your own operators. So, technically, nothing stops you from writing your own insane vector class and get exactly the syntax you want. Behold the majesty of C++ which lets you have your way without listening to reason; I get goosebumps sometimes:

#include <cstddef>
#include <vector>

class MyStrangeVector {
public:
void add(QLineEdit *le) {
m_vec.push_back(le);
}

// Index using () and dereference, just because we can
QLineEdit &operator()(size_t index) {
return *m_vec[index];
}

private:
std::vector<QLineEdit *> m_vec;
};

void f() {
MyStrangeVector LineEdit;

// Populate the vector
for (size_t i = 0; i < 1650; ++i) {
QLineEdit *le = new QLineEdit(/* ... */);
// ...
LineEdit.add(le);
}

// All this nonsense so that we can finally write:
LineEdit(42).setText("meh");
}
Thanks. You've made my day.

Cool! Can you do it in python?

Added after 22 minutes:


For the nth (and last for me) time, you cannot access an existing LineEdit, subclassed or not, using the syntax LineEdit( item#).

You keep insisting that your way is the only way. Good luck with it.

Actually I never insisted that my way is the only way. I merely asked if it was possible to do it the way I wanted to and rather than the simple yes or no that I asked for, I got a whole bunch of suggestions to do something that would not accomplish what I need. Don't get me wrong, the discussion has been interesting and I may be able to use some of the answers I got, but I don't think that anyone has actually understood what I am trying to accomplish and that has been frustrating for me because I have looked back over my prior posts and it seems very clear to me. :)

Added after 28 minutes:

Ok! So I can't use the syntax that I want to use. How about this?



itemNo = 12345
MyLineEdit = QLineEdit()
name = "MyLineEdit" + itemNo
MyLineEdit.setObjectName(name)


This would give me a LineEdit named MyLineEdit12345 and I could set the text with MyLineEdit12345.setText(1) and I could access the text with string = MyLineEdit12345.text()

anda_skoa
14th May 2016, 11:23
What does OP stand for?

Original Poster, i.e. you.



I got a very similar reply on a different list, using a dictionary. I will probably end up doing something like it but it seems like overkill for what I am trying to do.

Yes, a vector or list would be better.



Actually I never insisted that my way is the only way.

You insisted on a particular syntax that is not part of the language you've chosen.



I merely asked if it was possible to do it the way I wanted to and rather than the simple yes or no that I asked for, I got a whole bunch of suggestions to do something that would not accomplish what I need.

You've got suggestions to do exactly what you need.
The only difference between the suggested solution of using a linear container and your insisted syntax is the use of [] instead of () for the index access.


Don't get me wrong, the discussion has been interesting and I may be able to use some of the answers I got, but I don't think that anyone has actually understood what I am trying to accomplish and that has been frustrating for me because I have looked back over my prior posts and it seems very clear to me. :)

Well, we assume that you want to access the values that the user sees.
You insist that you want to work with temporary objects that are not the ones the user interacts with.

Usually a program that presents UI to the user will want to either change the values the user sees or the the user's input or both.



This would give me a LineEdit named MyLineEdit12345 and I could set the text with MyLineEdit12345.setText(1) and I could access the text with string = MyLineEdit12345.text()
No.
The object name property is a value of the object, not the name of the variable holding the object.

Seriously:
- create a list named "LineEdit"
- put your line edits into that list
- access the line edits via index using the list's [] operator

The only difference to your target syntax is usage of brackets instead of parentheses.

Cheers,
_

nlgootee
16th May 2016, 22:22
Original Poster, i.e. you.

No.
The object name property is a value of the object, not the name of the variable holding the object.

Cheers,
_

What is the name of the variable holding the object? I thought that it was the objectname.

anda_skoa
17th May 2016, 05:46
What is the name of the variable holding the object?

Hmm, I would have assumed you had had a look at some Python tutorial before starting on an actual application.

The name of the variable is the identifier you are writing on the left side of the assignment expression.


foo = QLineEdit()

Local variable named "foo"


self.foo = QLineEdit()

Class instance variable named "foo"



I thought that it was the objectname.
The object name is a property of the object provided through its QObject ancestry, similar to how the font is provided through its QWidget ancestry, etc.

Cheers,
_

yeye_olive
17th May 2016, 10:54
Cool! Can you do it in python?
I suppose you could allocate a container (an array, list, whatever) that maps integers to QLineEdits. You would fill it with the QLineEdit instances at initialization, and you would define a function named LineEdit that takes an integer i, finds the QLineEdit in the container with index i, and returns it. I do not know much about Python, but it looks like it would let you do LineEdit(i).setText("foo").

nlgootee
19th May 2016, 18:27
Hmm, I would have assumed you had had a look at some Python tutorial before starting on an actual application.

The name of the variable is the identifier you are writing on the left side of the assignment expression.


foo = QLineEdit()

Local variable named "foo"


self.foo = QLineEdit()

Class instance variable named "foo"


The object name is a property of the object provided through its QObject ancestry, similar to how the font is provided through its QWidget ancestry, etc.

Cheers,
_

In the Qt designer the name that you use to access the object in code is set by the objectname

yeye_olive
20th May 2016, 09:54
In the Qt designer the name that you use to access the object in code is set by the objectname
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

myTextEdit->setText("Hello");
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.

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.

nlgootee
20th May 2016, 16:59
self.swInventory = QtWidgets.QStackedWidget(self.frInventory)
self.swInventory.setObjectName("swInventory")
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.swInventory.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.swInventory.addWidget(self.page_2)

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?

anda_skoa
23rd May 2016, 10:02
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?

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.



Can I just use the same identifier (self.Page) with a different objectName for each page?
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,
_

yeye_olive
23rd May 2016, 17:09
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])

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?

ReshmaRatan
25th May 2016, 07:58
No, I actually don't need to store them. I will try to explain more clearly how I want this to work. I create a column of 1650 rows that consist of a QLineEdit(call it MyLineEdit(item#)) and 2 or 3 Labels (item#, description, price) the lineedit and labels are indexed with the same number. The salesperson while making an order scrolls down the column and at each item in the order places a number(quantity) in the LineEdit. When the order is complete, the text of the LineEdit and the labels is placed in the invoice and saved in a database and the text is cleared from all of the LineEdits to be ready for the next order. The LineEdits exist on the screen until the application is closed, the text is only there until it is saved in the database. If in the future, someone needs to look at the order, a query is run using the OrderID and the order is recreated placing the quantity of each item in the order in the LineEdits using MyLineEdit(item#).setText(). The LineEdits are created at the beginning of the application and destroyed when it closes, I don't need to store them anywhere. When the order is created, I can use the textChanged signal to store the information and probably don't actually need the item# index, but when the order is recreated being able to use MyLineEdit(item#).setText() is much better than the way it was done in the original application. I hope that this helps to clear thing up. I really appreciate your help with this. Thanks

Hello,

This is my first post in this forum. So please don't mind if any mistakes.
Coming to the problem, as per my understanding in a short way, you have many rows and each row has a line edit where user can enter some value. At last u have to get all the values from all the line edits. And those line edits are dynamically created in a for loop.

I have worked on the similar scenario where i have some buttons which are dynamically created in a for loop and i need to understand on fly which button is triggered.

For this i have used QSignalMapper concept. Below is the sample code.


//Code Begins
QSignalMapper *m_btnSigMapper; //its a class variable
for(int nIndex = 0; nIndex < 10; nIndex++)
{
//create buttons
QPushButton *btn = new QPushButton();
//set the properties as required.
connect(btn , SIGNAL(clicked()), m_btnSigMapper, SLOT(map()));
//now create unique name for your widget
QString szMapName = QString::number(nIndex);
m_btnSigMapper->setMapping(btn , szMapName);
}
//now this is important connect
//Here we connect to our slot whenever a button is clicked.
connect(m_btnSigMapper, SIGNAL(mapped(const QString &)), this, SLOT(slotBtnClicked(const QString &)));

//slotBtnClicked function
void slotBtnClicked(QString szMapName)
{
//do the functionality based on the map name because it is unique.
}

////// A sample code for your problem statement can be like below.

QSignalMapper *m_lineEditSigMapper; //its a class variable
for(int nIndex = 0; nIndex < 10; nIndex++)
{
//create line edits
QLineEdit *lineEdit = new QLineEdit();

//now set a name to your line edit.
lineEdit.setObjectName(QString("LineEdit").append(QString::number(nIndex)));

//set the properties as required.

//Connect textEdited signal.
connect(lineEdit , SIGNAL(textEdited(const QString &)), m_lineEditSigMapper, SLOT(map()));

m_lineEditSigMapper->setMapping(lineEdit , lineEdit);
}
//now this is important connect
//Here we connect to our slot whenever a text is edited in your line edits.
connect(m_lineEditSigMapper, SIGNAL(mapped(QWidget*)), this, SLOT(slotLineEditTextEdited(QWidget*)));

//Create a variable which stores the values from LineEdits.
QMap<QString,QString> m_lineEditValues; //class variable
//slotLineEditTextEdited function
void slotLineEditTextEdited(QWidget* widget)
{
QLineEdit *lineEdit = reinterpret_cast<QLineEdit*> (widget);
m_lineEditValues.insert(lineEdit.objectName(),line Edit.text());
}

Cheers...

nlgootee
2nd June 2016, 16:18
Thanks for your input. This looks like something that I could use for a column of pushbuttons, but I really don't translate c++ to python very well, so I will have to work on it.

Added after 4 minutes:

I want to thank everybody that submitted code to this discussion. We have decided to go with a different method but your expertise will not go to waste because the next part of the project absolutely needs a list and I will be able to use what I have learned here. Thanks again.

nlgootee
4th June 2016, 00:58
I forgot to mention that the program that I am converting has a column of pushbuttons that I have to deal with, so really, thanks.