How can I draw an ellipse using QPainter?
I want to draw ellipse in UI but my code doesn't work.
QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1
Mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow), pm(100,100)
{
ui->setupUi(this);
//set_form_style();
draw_ellipse();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::set_form_style(){
//setWindowFlags(Qt::FramelessWindowHint);
//setAttribute(Qt::WA_TranslucentBackground);
//ui->widget->setStyleSheet("background:transparent;");
setMouseTracking(true);
}
void MainWindow::draw_ellipse(){
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush(Qt::red, Qt::SolidPattern));
painter.drawEllipse(100, 50, 150, 150);
}
The issue isn't the way you're using the QPainter
, it's the time you're obtaining it.
As the reference documentation says, "the common use of QPainter is inside a widget's paint event". So if you want to do custom painting in your main window, override paintEvent
and put your code there.