QPainter painter(widget); // create a painter that draws on a widget painter.setOpacity(0.5); // set the opacity to 50% painter.drawRect(10, 10, 100, 100); // draw a translucent rectangle
QPainter painter(widget); // create a painter that draws on a widget for (int i = 0; i < 10; i++) { painter.setOpacity(i / 10.0); // set the opacity to 0.0 for i = 0 and 1.0 for i = 9 painter.drawText(QPointF(10 + i * 10, 50), "Hello world"); // draw the text at a different location and opacity for each loop iteration }In this example, we use a loop to draw the text "Hello world" multiple times at different locations with varying opacity. We use setOpacity to set the opacity level to a value between 0.0 and 1.0 based on the value of i. The setOpacity function is provided by the QtGui module of the Qt library.