Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML Array Undefined Check

Tags:

qt

qml

I am getting an array undefined error in QML and am wondering what will fix this error. Here is the current code:

opacity: mBitField[index]

every once and a while it will say cannot assign undefined to opacity and I am wondering if this fix is valid:

opacity: mBitField[index] == "undefined" ? 0 : mBitField[index]

Basically what I am trying to say is if the array is undefined it's ok to assign a 0 opacity otherwise assign whats in the array.

like image 926
Christopher Peterson Avatar asked Oct 20 '11 19:10

Christopher Peterson


1 Answers

Your code converts the array element to a string which is not necessary. To check for undefined only it would be:

opacity: mBitField[index] === undefined ? 0 : mBitField[index]

Or if distinguishing undefined and null (and false, 0 or "") is not important, just use the power of Javascript :)

opacity: mBitField[index] || 0
like image 147
blakharaz Avatar answered Sep 27 '22 17:09

blakharaz