app crash when slider connect to paintEvent

  • hi everyone, i need help, i'm trying to create donut chart with paintEvent and i will connecting that donut bar with slider to simulate.
    when i sliding the slider the run app crash, anyone tell me what's wrong
    i connecting slider with this


    this is my script in mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QWidget>
    #include "donutr.h"
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        donutr *r;
        Ui::MainWindow *ui;
    public slots:
        void setvar(int val){r->setdonut(val);}
    #endif // MAINWINDOW_H

    and this is my script in promote widget donutr.h

    #ifndef DONUTR_H
    #define DONUTR_H
    #include <QWidget>
    class donutr : public QWidget
        donutr(QWidget *parent);
        int setdonut(int val) {
        int data = val;
       int data;
        void paintEvent(QPaintEvent *event);
    #endif // DONUT_H

    and this is the donutr.cpp

    #include "mainwindow.h"
    #include <QPainter>
    donutr::donutr(QWidget *parent) : QWidget(parent)
    void donutr::paintEvent(QPaintEvent *event)
        QPainter painter(this);
        QColor warna1 = QColor(230, 230, 230);
        QRectF size1 = QRectF(0,30,300,300);
        QRectF size2 = QRectF(26,56,250,250);
        painter.setBrush(QColor(255, 143, 145,255));
        painter.setPen(QColor(255, 143, 145,255));
        painter.drawPie(size1,0*16, data);

  • Lifetime Qt Champion

    Hi and welcome
    You have the widget as
    donutr *r;

    do you create an instance ?
    r= new donutr(this);


    Else you will crash here
    void setvar(int val){
    r->setdonut(val); <<<<< is r a valid object here?

  • @mrjj no, i don't create
    r= new donutr(this);

    i try to create
    r= new donutr(this);
    in mainwindow.h but some error show, i trying to move that script to mainwindow.cpp the error don't show agan and my app don't close
    thx for your info.

    now my script works

  • Lifetime Qt Champion

    Ok. A good place to create instance would been
    just after the setupUI in constructor of mainwindow.

    Just as note
    when you write
    donutr *r;

    You just declare it. A pointer to a donutr . but its not set to point to anything.
    So if u use it - without setting it to point to valid object. it will crash.

Log in to reply

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