Results 1 to 9 of 9

Thread: qtlist Query

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Question qtlist Query

    I have x, y variables that are constantly changing
    Only when user clicks a button will the x, y variable at that time be stored in array

    Created button & linking to function ->no problem

    not sure how to go about the array part
    this is my code:
    .h
    Qt Code:
    1. private:
    2. QList<int> ArrayX;
    3. QList<int> ArrayY;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void my_qlabel::transferValues()
    2. {
    3. //counter initialised to 0 in constructor
    4. Counter += 1;
    5. ArrayX[Counter] = x;
    6. ArrayY[Counter] = y;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Is the array Declared properly (array Has to be dynamic as it can increase as many time user pushes button)
    Do I have to use the .append???

    please provide me with sample code on qlist if possible?

    Thanks

  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: qtlist Query

    Call the append() method or the push_back() method or use the << operator

    Qt Code:
    1. ArrayX << x;
    2. ArrayY << y;
    To copy to clipboard, switch view to plain text mode 

    Might be even better to have only one list and have it store a structure that holds two values, e.g. a list of QPoint

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: qtlist Query

    Yes you have to use append(). You don't need a counter.
    Just do this -
    Qt Code:
    1. ArrayX.append(x);
    2. ArrayY.append(y);
    3.  
    4. //To access
    5. ArrayX.at(index);
    6. ArrayY.at(index);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Arrow Re: qtlist Query

    Thanks

    Once array is completed its written to a file.txt

    I need it to iterate through the list
    & once completed all arrays must be cleared.

    This is what I got so far:

    Qt Code:
    1. ...//[I]open file usual way[/I]
    2. ...//[I]create cout->stream[/I]
    3. QTextStream stream(&file);
    4.  
    5. stream << Counter << "\n";
    6.  
    7. //prints to file -> x, y
    8. int i = 0;
    9. while(i <= Counter)
    10. {
    11. stream << ArrayX[i] << "\t" <<ArrayY[i] << "\n";
    12. i++;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Is the output (stream) syntax correct
    Need a counter for iterating while printing to file, Correct?
    qDeleteAll() example, pls

    output should be:
    [Total points]
    [list of points on seperate lines]

    Thanks for the quick response
    Last edited by ebsaith; 13th June 2013 at 11:26. Reason: updated contents

  5. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: qtlist Query

    Qt Code:
    1. int i = 0;
    2. while( i < ArrayX.count() ) // ArrayX.size() also gives the total number of items
    3. {
    4. i++;
    5. //
    6. }
    To copy to clipboard, switch view to plain text mode 

    Make sure the ArrayX.count() & ArrayY.count() are equal if you are using values of both the arrays inside while.
    Last edited by rawfool; 13th June 2013 at 14:16.

  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: qtlist Query

    Quote Originally Posted by rawfool View Post
    Make sure the ArrayX.count() & ArrayY.count() are equal if you are using values of both the arrays inside while.
    Indeed. Hence my earlier suggestion to use a single array that contains both values in each entry

    Quote Originally Posted by ebsaith View Post
    qDeleteAll() example, pls
    What for? Your containers (lists) do not contain any pointers.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: qtlist Query

    Yes anda_skoa, you were spot on. He should probably use list of QPoint like you mentioned. But my answer was for the current situation

  8. #8
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Arrow Re: qtlist Query

    Im not sure if data is being stored in my array.
    and when Itry to create a file displaying array --- program crashes

    Heres my code: for sending array variables to filestream
    my feeling the error is in this statement, not sure how to fix it?
    Qt Code:
    1. int i = 0;
    2. while( i <= ArrayX.count() )
    3. {
    4. stream << ArrayY[i] << "\t" << ArrayX[i] << endl;
    5. i++;
    6. }
    To copy to clipboard, switch view to plain text mode 

    setting variables in array as above reply/post thread

    please help
    Last edited by ebsaith; 13th June 2013 at 13:30. Reason: updated contents

  9. #9
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: qtlist Query

    Bound check : i < ArrayX.count()

  10. The following user says thank you to nix for this useful post:

    rawfool (13th June 2013)

Similar Threads

  1. sql query problem
    By unix7777 in forum Newbie
    Replies: 10
    Last Post: 29th August 2012, 21:04
  2. QTList Custom class Serlization
    By saravanadel in forum Newbie
    Replies: 12
    Last Post: 20th January 2012, 21:49
  3. unable to run query
    By sachinmcajnu in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2011, 12:14
  4. SQL query
    By JD2000 in forum Newbie
    Replies: 4
    Last Post: 1st December 2009, 14:21
  5. MS SQL Query
    By baray98 in forum General Programming
    Replies: 0
    Last Post: 14th July 2009, 04:25

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.