Reputation: 84
in first for loop when I wrote z.size() I get
Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there.
and also in second for when I wrote z.size I get 10 output but size of z is 5 as you can see. What is wrong .First 5 output is 0 and then rest is normal like 0 1 2 3 4 but I should have 5 output am I wrong
QVector<int> z(5);
for(int i=0;i<5;i++)
z.push_back(i);
QString str;
for (int i = 0; i < z.size(); ++i)
{
if (i > 0)
str += " ";
str += QString::number(z[i]);
}
ui->mbox->setText(str);
}
Upvotes: 0
Views: 492
Reputation:
When you create the QVector z, you give the initial size of 5. That means that the vector contains 5 zeroes. Then you add five more ints, from 0 to 4. The fix is to change
QVector<int> z(5);
to
QVector<int> z;
Most often, QVector is not the best container to use, usually QList is much better. From Qt's documentation:
For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable.
Upvotes: 1
Reputation: 16164
for(int i=0;i<5;i++)
z.push_back(i);
You just added 5 elements into the array.
Upvotes: 0