Page 1 of 2 12 LastLast
Results 1 to 20 of 38

Thread: subclass QLineEdit to have an index

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

    Default subclass QLineEdit to have an index

    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?

  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

    So is this the same LineEdit object? What would the index do?
    Or are these three LineEdit objects?

    Cheers,
    _

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

    Default Re: subclass QLineEdit to have an index

    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?

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

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

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

    Default Re: subclass QLineEdit to have an index

    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.

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

    What does the class do with the index number?

    Cheers,
    _

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

    Default Re: subclass QLineEdit to have an index

    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.

  8. #8
    Join Date
    May 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
    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:


    Quote Originally Posted by d_stranz View Post
    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
    Last edited by nlgootee; 3rd May 2016 at 23:50.

  9. #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
    The class doesn't do anything with the index.
    Then why does it need the index?

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

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

    Quote Originally Posted by nlgootee View Post
    What is a vector?
    A vector is a linear container, like a list or array.
    Its elements can be accessed by index.

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

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

  10. #10
    Join Date
    May 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.

  11. #11
    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,
    _

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    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 )".

  13. #13
    Join Date
    May 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.

  14. #14
    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,
    _

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    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.

  16. #16
    Join Date
    May 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 18:19.

  17. #17
    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,
    _

  18. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    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 19:19.

  19. #19
    Join Date
    May 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

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

Similar Threads

  1. Replies: 0
    Last Post: 11th June 2013, 11:50
  2. Replies: 2
    Last Post: 15th April 2013, 07:33
  3. Index out of bounds in custom QLayout subclass
    By space_otter in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2011, 21:23
  4. Replies: 1
    Last Post: 12th January 2011, 23:40
  5. Replies: 8
    Last Post: 12th February 2010, 03: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.