Results 1 to 5 of 5

Thread: A help with QCache?

  1. #1
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default A help with QCache?

    I am trying to use the cache to store/retrieve data but I cannot actually figure it out how?
    I didn't made a dedicated example for it so I just tested it with a login form, trying to store the user inside the cache and retrieve it on QDialog::exec().
    I made a class
    Qt Code:
    1. class InfoUtil{
    2. public:
    3. QString nUtil;
    4. void setUtil(QString a);
    5. const char* retUtil();
    6. };
    7.  
    8. void InfoUtil::setUtil(QString a){
    9. nUtil=a;
    10. }
    11. const char* InfoUtil::retUtil(){
    12. return nUtil.toStdString().c_str();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Here is the block which checks if the login credentials are ok, and if they are it does emits some signals etc and cache the login user
    Qt Code:
    1. if(util=="admin" && parola=="admin"){
    2. QCache<const char*,InfoUtil> cache;
    3. iu->setUtil(util);
    4. cache.clear();
    5. cache.insert(iu->retUtil(),iu);
    6. }
    To copy to clipboard, switch view to plain text mode 


    util is a string which gets it's data from QLineEdit.
    When the connection dialog opens it should have the cached "admin" set to the QLineEdit, but I get symbols.
    Here is how I try to retrieve the data and set it to QLineEdit inside the dialog's constructor.
    Qt Code:
    1. QCache<const char*,InfoUtil> cache;
    2. const char* abc;
    3. cache.take(abc);
    4. txtUtil=new QLineEdit(abc);
    To copy to clipboard, switch view to plain text mode 

    I know it's wrong but can someone send me to the right direction?

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

    Default Re: A help with QCache?

    Where to start...

    To retrieve a value from QCache requires the same QCache you put the value in in the first place. In you code the QCache you insert into ceases to exist at the end of the if() block. The QCache you try to read from is a different instance, and one that cannot contain anything when you try to get stuff out of it.

    In your read code you use a char* uninitialised (a fault) as cache key, ignore the return value from take() (the very thing take() exists to give you), and then use the unchanged uninitialised pointer to set the label text.

    InfoUtil::retUtil() returns an unsafe pointer to the buffer of an object that has been destroyed.

    QCache is not the right tool for storing the current value of anything you cannot afford to lose. QCache will delete values that have not been recently used if it needs to free space (it is size limited).

    Perhaps you can explain what you are trying to achieve.

  3. The following user says thank you to ChrisW67 for this useful post:

    adutzu89 (4th March 2014)

  4. #3
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: A help with QCache?

    I'm trying to store some user info after he log's in, like it's ID and category until he logs out or close the program.
    Something that can be persistant throught the program(meaning I can acces it from different classe's constructors or functions).

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A help with QCache?

    I doubt you want to use a cache then. A cache can throw away entries when it needs space for new entries.

    What you want is something that keeps the entry until you remove it explicitly (when the user logs out).

    See QMap or QHash

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    adutzu89 (4th March 2014)

  7. #5
    Join Date
    Oct 2013
    Posts
    142
    Thanks
    36
    Thanked 3 Times in 3 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: A help with QCache?

    Thank you for both your answers.

Similar Threads

  1. QCache maxcost not respected
    By maitai in forum Qt Programming
    Replies: 10
    Last Post: 28th February 2011, 11:09
  2. QCache serialization
    By Flakes in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2011, 17:44
  3. QtConcurent and QCache
    By baray98 in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2010, 08:45

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.