The key concept refers to dictionaries (dict, map), and for lists, arrays and sets (list, array, set), access to the elements is done through an index that starts from 0 Π΄ΠΎ <ΡΠ°Π·ΠΌΠ΅Ρ - 1> or from 1 Π΄ΠΎ ΡΠ°Π·ΠΌΠ΅Ρ .
Therefore, to check the availability of the index, you need to refer to the size and compare the size with the index.
QList has 3 identical functions for checking the size: count, size and length.
For your example, add a verification:
int index = 4; // ΠΠ»Ρ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΡΠΈΠΈ ΠΎΡ 0 Π΄ΠΎ n - 1 // Π Π΄Π»Ρ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΡΠΈΠΈ ΠΎΡ 0 Π΄ΠΎ n, ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° ΠΈΠ·ΠΌΠ΅Π½ΠΈΡΡΡ Π½Π°: index <= ppc.size() if (index < ppc.size()) { int res = ppc.at(index); // ΠΈΠ»ΠΈ ppc[index] }
QList with an incorrect index does not throw an exception (as historically in Qt), but, for example, in java there is an ArrayIndexOutOfBoundsException , and in python it is an IndexError .
Then, instead of checking, you can catch an exception, for example, then the code with checking would change to something like this:
int index = 4; try { int res = ppc.at(index); } catch (IndexError) { // ignore }