QColor color(255, 0, 0, 128); // red color with 50% transparency if (color.isValid()) { // valid color } else { // invalid color }
QColor generateRandomColor() { while (true) { int r = rand() % 256; int g = rand() % 256; int b = rand() % 256; int a = rand() % 256; QColor color(r, g, b, a); if (color.isValid()) { return color; } } }The package library for QColor isValid is Qt, which is a cross-platform application development framework.