AAAH! Origin to both bugs is your random data.. there is of course sometimes a 4 inside!!

int(rand()/float(RAND_MAX)*4).

As the random generator is deterministic without initialization.. this always occured at the same value..

quick fix: quint8 base = qMin((quint8)3,data.at(i).at(j)); in the paint method

So now everything works.. and blazing fast :->

Good luck for your project!

Johannes