#include#include int main(int argc, char *argv[]) { QApplication app(argc, argv); QPixmap pixmap(400, 300); pixmap.fill(Qt::white); QPainter painter(&pixmap); painter.setPen(QPen(Qt::black, 2)); painter.drawRect(50, 50, 300, 200); painter.drawText(125, 125, "Hello, world!"); painter.end(); pixmap.save("output.png"); return app.exec(); }
#includeThis example creates a custom QMainWindow subclass with a paintEvent function that uses a QPainter object to draw a black rectangle and some text directly on the window. The window is then shown to the user. Package library: Qt.#include #include class MyWindow : public QMainWindow { public: MyWindow() { resize(400, 300); } protected: void paintEvent(QPaintEvent *event) override { Q_UNUSED(event) QPainter painter(this); painter.setPen(QPen(Qt::black, 2)); painter.drawRect(50, 50, 300, 200); painter.drawText(125, 125, "Hello, world!"); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWindow window; window.show(); return app.exec(); }