How to parent QPainter to QTabWidget?
Solved
General and Desktop
-
Hi!
I'm creating paint software which has tabs with Qt.
I want to add the QPainter to each tab(Created with the QTabWidget).
But the QPainter's function doesn't have the "QWidget *parent" property.
How should I do?
(I'm Japanese, so please forgive my poor English.) -
Hi
Use a custom widget for each tab so you can override paintEvent.#include <QPainter> #include <QWidget> class Canvas : public QWidget { Q_OBJECT public: explicit Canvas(QWidget *parent = nullptr) : QWidget(parent) { } protected: virtual void paintEvent(QPaintEvent *event) override { QPainter p(this); p.drawRect(0, 0, 100, 100); } };
-
You can't attach a painter to something. You can create a QPainter in the QWidget::paintEvent() to draw on the widget.
-
Hi
Use a custom widget for each tab so you can override paintEvent.#include <QPainter> #include <QWidget> class Canvas : public QWidget { Q_OBJECT public: explicit Canvas(QWidget *parent = nullptr) : QWidget(parent) { } protected: virtual void paintEvent(QPaintEvent *event) override { QPainter p(this); p.drawRect(0, 0, 100, 100); } };