View Full Version : qtlist Query
ebsaith
13th June 2013, 11:42
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
private:
QList<int> ArrayX;
QList<int> ArrayY;
void my_qlabel::transferValues()
{
//counter initialised to 0 in constructor
Counter += 1;
ArrayX[Counter] = x;
ArrayY[Counter] = y;
}
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
anda_skoa
13th June 2013, 12:05
Call the append() method or the push_back() method or use the << operator
ArrayX << x;
ArrayY << y;
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,
_
rawfool
13th June 2013, 12:06
Yes you have to use append(). You don't need a counter.
Just do this -
ArrayX.append(x);
ArrayY.append(y);
//To access
ArrayX.at(index);
ArrayY.at(index);
ebsaith
13th June 2013, 12:25
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:
...//open file usual way
...//create cout->stream
QTextStream stream(&file);
stream << Counter << "\n";
//prints to file -> x, y
int i = 0;
while(i <= Counter)
{
stream << ArrayX[i] << "\t" <<ArrayY[i] << "\n";
i++;
}
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:)
rawfool
13th June 2013, 13:16
int i = 0;
while( i < ArrayX.count() ) // ArrayX.size() also gives the total number of items
{
i++;
//
}
Make sure the ArrayX.count() & ArrayY.count() are equal if you are using values of both the arrays inside while.
ebsaith
13th June 2013, 14:30
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?
int i = 0;
while( i <= ArrayX.count() )
{
stream << ArrayY[i] << "\t" << ArrayX[i] << endl;
i++;
}
setting variables in array as above reply/post thread
please help
Bound check : i < ArrayX.count()
anda_skoa
13th June 2013, 15:02
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 :)
qDeleteAll() example, pls
What for? Your containers (lists) do not contain any pointers.
Cheers,
_
rawfool
13th June 2013, 15:15
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 :)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.