#ifndef VIEW_H #define VIEW_H #include QT_BEGIN_NAMESPACE namespace Ui { class View; } QT_END_NAMESPACE class View : public QWidget { Q_OBJECT public: View(QWidget *parent = nullptr); ~View(); private: Ui::View *ui; // QWidget interface protected: void paintEvent(QPaintEvent *event) override; }; #endif // VIEW_H #include "view.h" #include "ui_view.h" #include View::View(QWidget *parent) : QWidget(parent) , ui(new Ui::View) { ui->setupUi(this); } View::~View() { delete ui; } #include void View::paintEvent (QPaintEvent *event) { int w = width (); int h = height (); QPainter p (this); QLinearGradient g (QPointF(0, 0), QPointF(w, h)); g.setColorAt (0, QColor ("red")); g.setColorAt (1, QColor ("lime")); p.setPen (QPen (QColor ("blue"), 3)); p.setBrush(QBrush (g)); // p.drawLine (0, 0, 100, 100); p.drawEllipse (0, 0, w, h); } int main (int argc, char *argv[]) { QApplication a(argc, argv); View w; w.show(); return a.exec(); } QT += core gui widgets # CONFIG += c++11 SOURCES += view.cc HEADERS += view.h FORMS += view.ui import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Widget (QWidget) : def __init__ (self) : super().__init__() def color (self, painter, r, g, b) : painter.setPen (QColor.fromRgb (r, g, b)) def line (self, painter, x1, y1, x2, y2) : painter.drawLine (x1, y1, x2, y2) def paintEvent (self, event) : # QPaintEvent *event p = QPainter () p.begin (self) w = self.width () h = self.height () self.color (p, 255, 0, 0) self.line (p, 0, 0, w-1, 0) self.color (p, 0, 255, 0) self.line (p, w-1, 0, w-1, h-1) self.color (p, 0, 0, 255) self.line (p, w-1, h-1, 0, h-1) self.color (p, 255, 200, 0) self.line (p, 0, h-1, 0, 0) p.end () if __name__ == "__main__" : app = QApplication (sys.argv) win = Widget () win.show () sys.exit (app.exec_())