×

Discussion Board

Results 1 to 9 of 9

Thread: QSet lost items

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    11

    QSet lost items

    Hi,

    I think I'm going crazy :

    There are 2 methods
    1 I made a QSet I fill in with a random method ==> the local Qset is made of 10 items
    1 I made a QSet that I declare to be filled with the previous one.

    ==> The retrieved QSet is made of 6 items !!
    A breakpoint did not allowed me to find anything about this

    the code is
    Code:
    QSet <int> Keyboard_Dialog::RandomKeys ()
    {
        QSet <int> Keys_Set                 ;
        QTime time = QTime::currentTime()   ;           // Create seed for the random that is needed only once on startup
        int MAX_KEYS_NB = 10                ;           // define the max nr of keys to generate in the keyboard
        int RandomValue = 0, index          ;
    
        // qsrand((uint)time.msec())           ;
    
        qsrand(QDateTime::currentDateTime ().toTime_t ());
    
    
        for (index = 0; index < MAX_KEYS_NB ; index++)
        {
          RandomValue = qrand()%10 ;
          Keys_Set.insert(RandomValue)      ;
        }
    
        return Keys_Set                     ;
    }
    
    
    void Keyboard_Dialog::Generate_Keys ()
    {
    
       QSet <int> Scrambled_Keyb_Set ;
    
       QSetIterator<int> i (Scrambled_Keyb_Set);                 // Java-style iterator to navigate through the set 
       QString Key_As_String ;
    
       this->Define_BtnID ();                                   // Assign all buttons a unique ID
       Scrambled_Keyb_Set = this->RandomKeys () ;
       this->ui->lineEdit->setText (QString::number (this->RandomKeys().size ()));  // 6 values in !!!
    
       foreach (const int &value, Scrambled_Keyb_Set )
            this->ui->buttonGroup->button(value)->setText(QString::number(value));
           //this->ui->lineEdit->setText (this->ui->lineEdit->text () + QString::number (value));
    }

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: QSet lost items

    You do realize that a QSet will enforce uniqueness, right. So if you insert "6" twice, only one "6" will be in the set. That's the definition of sets (though oddly the iPhone NSSet doesn't do that).

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    11

    Re: QSet lost items

    You do realize that a QSet will enforce uniqueness
    Yes that's what I want

    So if you insert "6" twice, only one "6" will be in the set.
    That' ok a if (contains) is missing, I understant but this is not my issue nevertheless . 6 is always the number of the items, so as it s like a "Random" generator this shouldn't happend every time : determinism you know.

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: QSet lost items

    Have you printed out the individual values the RNG is producing?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    11

    Re: QSet lost items

    Quote Originally Posted by danhicksbyron View Post
    Have you printed out the individual values the RNG is producing?
    yes : in RandomKeys they are ok, with 10 items in the set.

    In fact, there is something really weird I found out : the this->Define_BtnID (); function I call, is a simple method to initialize a Buttongroup with
    all the 10 QPushButton with a unique ID.

    If I move this call <after> the call, i.e. :

    Code:
     Scrambled_Keyb_Set = this->RandomKeys () ;
     this->Define_BtnID ();                                   // Assign all buttons a unique ID
    Now there are 8 items in the set...

  6. #6
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: QSet lost items

    You say there are ten values, but what are they? The odds of getting ten values that are all unique in the low digit are pretty small.

    I think your loop in RandomKeys, rather than being:

    for (index = 0; index < MAX_KEYS_NB ; index++)

    should be:

    while(KeySet.count() < MAX_KEYS_NB)

    Keep in mind that reinitializing the RNG on every call isn't generally a good idea, especially when you're initializing with a "seconds" value, since you could easily make all your calls in one second's time and get identical values. Instead, initialize it once, at the start of your app, or at least use a finer-grain initializer.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    11

    Re: QSet lost items

    a little better : I changed for that
    Code:
        while( nb_val < MAX_KEYS_NB)
            {
              //RandomValue = Randomize( 0, MAX_KEYS_NB );   // Get random value between 0-10
    
              RandomValue = qrand()%10          ;
              Keys_Set.insert(RandomValue)      ;
              nb_val = Keys_Set.count() ;
             }
    and later in the generate keys method :

    Code:
    QTime time = QTime::currentTime()   ;           // Create seed for the random that is needed only once on startup
       //qsrand(QDateTime::currentDateTime ().toTime_t ());
     qsrand((uint)time.msec())           ;
    
       Scrambled_Keyb_Set = this->RandomKeys () ;
       QList<int> list = Scrambled_Keyb_Set.toList();
    I moved the qsrand so that it initialize only on the QPushbutton event that is far to the mS.

    You say there are ten values, but what are they?
    an ordered set only, and always

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    11

    Re: QSet lost items

    Found the problem : The randomization is ok, the function is ok.
    This came from the QSet itself : even if it unordered as said in the Qt doc. there is nevertheless an internal ordering that the QSet (hash) uses . so
    using a List resolved the problem.

    Thank you very much for your help, even if this was't the solution it guided me to it .

  9. #9
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: QSet lost items

    If you'd told us from the start why you wanted this list and how it was being used it would have made things a lot easier.

Similar Threads

  1. Lost in cryptography
    By MrAlt in forum Symbian
    Replies: 12
    Last Post: 2010-03-24, 05:04
  2. lost and need help...
    By wwwsam in forum [Archived] Flash Lite on Nokia Devices
    Replies: 2
    Last Post: 2010-03-23, 06:13
  3. HTTP Connection behaviour when 3G connection is lost (or being lost)
    By TwmD in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2010-01-27, 12:36
  4. Items in a List of items
    By Alfury in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2008-06-24, 03:58
  5. Totally Lost
    By antonvdmerwe in forum General Messaging
    Replies: 2
    Last Post: 2002-08-22, 12:01

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×