I am creating many labels based inside a loop using new
operator in Qt, something like this:
QLabel *p;
for(int i=0; i<10; i++)
{
p = new QLabel(this);
// ...
}
Now what is happening, when I am using p
it points to the last label in the sequence but I want to use my first label. How can I keep track of every label?
You could store all Label-pointer using std::vector
:
std::vector<QLabel*> labels;
for (int i=0; i<10; i++)
labels.push_back(new QLabel(this));
Accessing these pointers can be done array-like:
QLabel *first = labels[0];
Or by using iterators:
std::vector<QLabel*>::iterator itr = labels.begin();
QLabel *first = *itr;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.