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
    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
    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.

  2. #2
    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

    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,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclass QLineEdit to have an index

    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 )".

  4. #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 d_stranz View Post
    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.

  5. #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

    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,
    _

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclass QLineEdit to have an index

    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.

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

    Default Re: subclass QLineEdit to have an index

    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:


    Quote Originally Posted by anda_skoa View Post
    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.
    Last edited by nlgootee; 6th May 2016 at 17:19.

  8. #8
    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
    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?

    Quote Originally Posted by nlgootee View Post
    I have a perfectly workable routine that uses the index to locate each LineEdit and retrieve the data from that row.
    Excellent.

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

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

    Quote Originally Posted by nlgootee View Post
    but first I want to know if it is possible to subclass a LineEdit with an index attribute
    Of course that is possible.

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

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclass QLineEdit to have an index

    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:

    Qt Code:
    1. LineEdit( index ).text()
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by d_stranz; 7th May 2016 at 18:19.

  10. #10
    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 d_stranz View Post
    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

  11. #11
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    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

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: subclass QLineEdit to have an index

    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.

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclass QLineEdit to have an index

    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.

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

    Default Re: subclass QLineEdit to have an index

    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()

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclass QLineEdit to have an index

    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.

  16. #16
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: subclass QLineEdit to have an index

    Quote Originally Posted by nlgootee View Post
    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.
    Qt Code:
    1. //Code Begins
    2. QSignalMapper *m_btnSigMapper; //its a class variable
    3. for(int nIndex = 0; nIndex < 10; nIndex++)
    4. {
    5. //create buttons
    6. QPushButton *btn = new QPushButton();
    7. //set the properties as required.
    8. connect(btn , SIGNAL(clicked()), m_btnSigMapper, SLOT(map()));
    9. //now create unique name for your widget
    10. QString szMapName = QString::number(nIndex);
    11. m_btnSigMapper->setMapping(btn , szMapName);
    12. }
    13. //now this is important connect
    14. //Here we connect to our slot whenever a button is clicked.
    15. connect(m_btnSigMapper, SIGNAL(mapped(const QString &)), this, SLOT(slotBtnClicked(const QString &)));
    16.  
    17. //slotBtnClicked function
    18. void slotBtnClicked(QString szMapName)
    19. {
    20. //do the functionality based on the map name because it is unique.
    21. }
    22.  
    23. ////// A sample code for your problem statement can be like below.
    24.  
    25. QSignalMapper *m_lineEditSigMapper; //its a class variable
    26. for(int nIndex = 0; nIndex < 10; nIndex++)
    27. {
    28. //create line edits
    29. QLineEdit *lineEdit = new QLineEdit();
    30.  
    31. //now set a name to your line edit.
    32. lineEdit.setObjectName(QString("LineEdit").append(QString::number(nIndex)));
    33.  
    34. //set the properties as required.
    35.  
    36. //Connect textEdited signal.
    37. connect(lineEdit , SIGNAL(textEdited(const QString &)), m_lineEditSigMapper, SLOT(map()));
    38.  
    39. m_lineEditSigMapper->setMapping(lineEdit , lineEdit);
    40. }
    41. //now this is important connect
    42. //Here we connect to our slot whenever a text is edited in your line edits.
    43. connect(m_lineEditSigMapper, SIGNAL(mapped(QWidget*)), this, SLOT(slotLineEditTextEdited(QWidget*)));
    44.  
    45. //Create a variable which stores the values from LineEdits.
    46. QMap<QString,QString> m_lineEditValues; //class variable
    47. //slotLineEditTextEdited function
    48. void slotLineEditTextEdited(QWidget* widget)
    49. {
    50. QLineEdit *lineEdit = reinterpret_cast<QLineEdit*> (widget);
    51. m_lineEditValues.insert(lineEdit.objectName(),lineEdit.text());
    52. }
    To copy to clipboard, switch view to plain text mode 
    Cheers...
    Last edited by anda_skoa; 25th May 2016 at 09:29. Reason: missing [code] tags

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

    Default Re: subclass QLineEdit to have an index

    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.
    Last edited by nlgootee; 2nd June 2016 at 16:18.

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

    Default Re: subclass QLineEdit to have an index

    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.

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.