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 thisconnect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(setvar(int)));
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 { Q_OBJECT private: 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 { Q_OBJECT public: donutr(QWidget *parent); int setdonut(int val) { int data = val; } private: int data; protected: 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) { Q_UNUSED(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); painter.setBrush(warna1); painter.setPen(warna1); painter.drawPie(size2,0,360*16); }
-
Hi and welcome
You have the widget as
donutr *r;do you create an instance ?
Like
r= new donutr(this);somewhere?
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
-
Hi
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.