Results 1 to 20 of 38

Thread: subclass QLineEdit to have an index

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    @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:
    Qt Code:
    1. #include <cstddef>
    2. #include <vector>
    3.  
    4. class MyStrangeVector {
    5. public:
    6. void add(QLineEdit *le) {
    7. m_vec.push_back(le);
    8. }
    9.  
    10. // Index using () and dereference, just because we can
    11. QLineEdit &operator()(size_t index) {
    12. return *m_vec[index];
    13. }
    14.  
    15. private:
    16. std::vector<QLineEdit *> m_vec;
    17. };
    18.  
    19. void f() {
    20. MyStrangeVector LineEdit;
    21.  
    22. // Populate the vector
    23. for (size_t i = 0; i < 1650; ++i) {
    24. QLineEdit *le = new QLineEdit(/* ... */);
    25. // ...
    26. LineEdit.add(le);
    27. }
    28.  
    29. // All this nonsense so that we can finally write:
    30. LineEdit(42).setText("meh");
    31. }
    To copy to clipboard, switch view to plain text mode 
    Thanks. You've made my day.

  2. The following user says thank you to yeye_olive for this useful post:

    d_stranz (10th May 2016)

  3. #2
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by ChrisW67 View Post
    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):
    Qt Code:
    1. self.TextBox = []
    2. for i in range(0, 1500):
    3. self.TextBox.append(new QLineEdit)
    4. layout.addWidget(self.TextBox[i])
    5.  
    6. # and later
    7. Str = TextBox[123].text()
    To copy to clipboard, switch view to plain text mode 
    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.

    Quote Originally Posted by yeye_olive View Post
    @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:
    Qt Code:
    1. #include <cstddef>
    2. #include <vector>
    3.  
    4. class MyStrangeVector {
    5. public:
    6. void add(QLineEdit *le) {
    7. m_vec.push_back(le);
    8. }
    9.  
    10. // Index using () and dereference, just because we can
    11. QLineEdit &operator()(size_t index) {
    12. return *m_vec[index];
    13. }
    14.  
    15. private:
    16. std::vector<QLineEdit *> m_vec;
    17. };
    18.  
    19. void f() {
    20. MyStrangeVector LineEdit;
    21.  
    22. // Populate the vector
    23. for (size_t i = 0; i < 1650; ++i) {
    24. QLineEdit *le = new QLineEdit(/* ... */);
    25. // ...
    26. LineEdit.add(le);
    27. }
    28.  
    29. // All this nonsense so that we can finally write:
    30. LineEdit(42).setText("meh");
    31. }
    To copy to clipboard, switch view to plain text mode 
    Thanks. You've made my day.
    Cool! Can you do it in python?


    Added after 22 minutes:


    Quote Originally Posted by d_stranz View Post
    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?

    Qt Code:
    1. itemNo = 12345
    2. MyLineEdit = QLineEdit()
    3. name = "MyLineEdit" + itemNo
    4. MyLineEdit.setObjectName(name)
    To copy to clipboard, switch view to plain text mode 

    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()
    Last edited by nlgootee; 13th May 2016 at 20:57.

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by nlgootee View Post
    What does OP stand for?
    Original Poster, i.e. you.

    Quote Originally Posted by nlgootee View Post
    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.

    Quote Originally Posted by nlgootee View Post
    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.

    Quote Originally Posted by nlgootee View Post
    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.

    Quote Originally Posted by nlgootee View Post
    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.

    Quote Originally Posted by nlgootee View Post
    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,
    _

  5. #4
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by anda_skoa View Post
    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.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by nlgootee View Post
    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.
    Qt Code:
    1. foo = QLineEdit()
    To copy to clipboard, switch view to plain text mode 
    Local variable named "foo"
    Qt Code:
    1. self.foo = QLineEdit()
    To copy to clipboard, switch view to plain text mode 
    Class instance variable named "foo"

    Quote Originally Posted by nlgootee View Post
    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,
    _

  7. #6
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by anda_skoa View Post
    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.
    Qt Code:
    1. foo = QLineEdit()
    To copy to clipboard, switch view to plain text mode 
    Local variable named "foo"
    Qt Code:
    1. self.foo = QLineEdit()
    To copy to clipboard, switch view to plain text mode 
    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

  8. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by nlgootee View Post
    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
    Qt Code:
    1. myTextEdit->setText("Hello");
    To copy to clipboard, switch view to plain text mode 
    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.

  9. #8
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: subclass QLineEdit to have an index

    Qt Code:
    1. self.swInventory = QtWidgets.QStackedWidget(self.frInventory)
    2. self.swInventory.setObjectName("swInventory")
    3. self.page = QtWidgets.QWidget()
    4. self.page.setObjectName("page")
    5. self.swInventory.addWidget(self.page)
    6. self.page_2 = QtWidgets.QWidget()
    7. self.page_2.setObjectName("page_2")
    8. 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?

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by nlgootee View Post
    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.

    Quote Originally Posted by nlgootee View Post
    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,
    _

  11. #10
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    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:
    Qt Code:
    1. values = []
    2. n = int(input("Please enter the number of integers: "))
    3. for idx in range(0, n):
    4. val = int(input("Please enter the value of the next integer: "))
    5. values.append(val)
    6. i = int(input("Please enter the index of the integer you want me to print: "))
    7. 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?

  12. #11
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by nlgootee View Post
    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").

Similar Threads

  1. Replies: 0
    Last Post: 11th June 2013, 10:50
  2. Replies: 2
    Last Post: 15th April 2013, 06:33
  3. Index out of bounds in custom QLayout subclass
    By space_otter in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2011, 20:23
  4. Replies: 1
    Last Post: 12th January 2011, 22:40
  5. Replies: 8
    Last Post: 12th February 2010, 02:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.