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 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
    Apr 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
    Apr 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,230
    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
    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
    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 22: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
    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.

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

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.