简体   繁体   中英

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);
}

why not just create an svg, add in the resource files and then use that svg anywhere you want to in label, image or whatever. I am new but I think it will get the job done because I have done the same thing this way before, but for you it could be different I cannot be so sure.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM