QPainter painter(this); QPen pen(Qt::red, 2); // create a red pen with width of 2 painter.setPen(pen); painter.drawLine(10, 10, 100, 100); // draw a line from (10,10) to (100,100)
QPainter painter(this); QPen pen(Qt::black); pen.setStyle(Qt::DashLine); // set pen style to dashed painter.setPen(pen); painter.drawRect(50, 50, 100, 100); // draw a rectangle with (50,50) as top-left corner and width and height of 100
QPainter painter(this); QFont font("Arial", 16); // create a font object with Arial font and font size of 16 painter.setFont(font); painter.drawText(10, 50, "Hello World!"); // draw the text string "Hello World!" starting at (10,50)In all examples above, the QPainter is created using the current widget as its surface. The QPainter object has various methods to draw different shapes and text, and the QPen object is used to set the attributes of the pen used to draw the shapes. The QtGui package is used for these examples.