QRectF rect(0, 0, -5, 10); if (rect.isValid()) { qDebug() << "Valid rectangle."; } else { qDebug() << "Invalid rectangle."; }
QGraphicsScene scene; QRectF rect(0, 0, 100, 50); if (rect.isValid()) { scene.addRect(rect, QPen(Qt::black), QBrush(Qt::red)); }In this example, a QRectF object is used to create a rectangle shape in a QGraphicsScene. The isValid function is used to make sure the rectangle is valid before adding it to the scene. The output will be a red rectangle with a black border. Package library: Qt.