Change color of circle in QPainter



  • I have the following code:

    QPixmap pm(100,100);
    pm.fill();
    QPainter p(&pm);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(QPen(Qt::blue,2));
    p.setBrush(QBrush(Qt::green));
    p.drawEllipse(10, 10, 80, 80);
    ui->label.setPixmap(pm);
    

    I would like to add the following if statements:

    if(x==0)
        p.setBrush(QBrush(Qt::white));
    else
        p.setPen(QPen(Qt::black,2));
    

    However, nothing changes and the output keeps telling me: QPaintDevice: Cannot destroy paint device that is being painted.
    Is there any to change the color of the border or inside of the color later on in the code?



  • No you can't change color later but if your funciton is paintEvent you can have variable of color.

    And you need to write p.end(); before ui->label ...



  • @Franckynos What do you mean by paintEvent? Can you give me an example?



  • Give me your all function.



  • @Franckynos Sorry?



  • @marlenet15

    Is there any to change the color of the border or inside of the color later on in the code?

    are you asking if you can change the colors after you called p.drawEllipse()? if thats you question, so you cannot. you cannot change the past.

    this is a valid code:

    QPixmap pm(100,100);
    pm.fill();
    QPainter p(&pm);
    p.setRenderHint(QPainter::Antialiasing, true);
    p.setPen(QPen(Qt::blue,2));
    p.setBrush(QBrush(Qt::green));
    if(x==0)
        p.setBrush(QBrush(Qt::white));
    else
        p.setPen(QPen(Qt::black,2));
    p.drawEllipse(10, 10, 80, 80);
    ui->label.setPixmap(pm);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.