Results 1 to 20 of 38

Thread: subclass QLineEdit to have an index

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #36
    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

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
  •  
Qt is a trademark of The Qt Company.