Results 1 to 4 of 4

Thread: How to get a value in a string list to match up with multiple indeces?

  1. #1
    Join Date
    May 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    2

    Question How to get a value in a string list to match up with multiple indeces?

    Hello all,

    suppose I had 4 stringlists for a store: Itemlist, Patronlist, DateBoughtList, DateReturnedList.


    And suppose I had it lined up like this:
    Qt Code:
    1. INDECES: 0 1 2 3 4
    2.  
    3. Item: LoTR HarryPotter 007 MarioKart Clothes
    4. Patron: Bob Sally Bob Bob Frank
    5. DateBought: march june july july august
    6. DateReturned: sept. sept oct dec november
    To copy to clipboard, switch view to plain text mode 

    I need to get a report that organizes this data by patron ( where each UNIQUE patron gets his/her own page with the appropriate items sorted out on)

    This is not a printing question, but what I was wondering is how can I group together the patron elements into unique elements, letting these elements retain multiple indeces (I would suspect QMultiHash, but am not too familiar with the API at this point.... still new at it)

    For example, if I were to isolate Bob: he has indeces 0,2, and 3. This would allow me to organize the data by printing out those lists at those indices on his patron page. Like so:

    Qt Code:
    1. Patron Bob(unique element 1):
    2.  
    3. **note that the items displayed on his page I am also trying to keep sorted by item for the program
    4. (item),(date bought),(date returned)
    5.  
    6. 007, july, oct
    7. LoTR..
    8. MarioKart..
    9.  
    10. Patron Frank(unique element 2)
    11.  
    12. clothes,..
    13.  
    14. etc.
    To copy to clipboard, switch view to plain text mode 

    based off of this, is there any way to make it so I can match a patron value with multiple indeces while being organized enough to conduct a report?

    ***if I am not being clear, let me know

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

    Default Re: How to get a value in a string list to match up with multiple indeces?

    You are clear enough, but perhaps you are approaching this the wrong way. You have four pieces of information about each transaction that must always be together to make sense. This is a classic case for a struct or class rather than trying to keep separate lists in sync:
    Qt Code:
    1. struct Txn {
    2. QString patron;
    3. QString item;
    4. QDate dateOut;
    5. QDate dateIn;
    6. // you could add a constructor to make creating these easier
    7. };
    8.  
    9. QMultiHash<QString, Txn> data; // map patron name to one or more transactions
    10. // or use QMultiMap if sorting by name is useful
    11.  
    12. // Add a transaction
    13. Txn txn;
    14. tx.patron = "Bob";
    15. txn.item = "Slinky";
    16. txn.dateOut = QDate(2012, 1, 1);
    17. txn.dateIn = QDate(2012, 2, 1);
    18. data.insert(txn.patron, txn);
    19.  
    20. // Access all transactions by Bob
    21. if (data.contains("Bob")) {
    22. foreach(const Txn &txn, data.values("Bob")) {
    23. qDebug() << tx.patron << txn.item << txn.dateOut << txn.dateIn;
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    This sort of data does lend itself to a relational database as an alternative.
    Last edited by ChrisW67; 8th May 2012 at 06:03.

  3. #3
    Join Date
    May 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    2

    Default Re: How to get a value in a string list to match up with multiple indeces?

    this helps for the most part, but i am not searching for a specific person, i am trying to iterate through all unique elements to put on my report

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

    Default Re: How to get a value in a string list to match up with multiple indeces?

    I am sure if you read the QMultiHash/QHash or QMultiMap/QMap docs you will find code snippets showing different ways to iterate over the data.

Similar Threads

  1. Multiple selection list in webkit qt
    By swagatdash in forum Newbie
    Replies: 0
    Last Post: 23rd June 2010, 15:51
  2. Replies: 11
    Last Post: 25th November 2008, 08:54
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 15:48
  4. Single slot for multiple list boxes
    By Sheetal in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 07:53
  5. multiple selection of list view items
    By samirg in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 20:05

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.