Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. app crash when slider connect to paintEvent
Qt 6.11 is out! See what's new in the release blog

app crash when slider connect to paintEvent

Scheduled Pinned Locked Moved Solved The Lounge
4 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pantat merah
    wrote on last edited by
    #1

    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

    connect(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);
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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?
      }

      P 1 Reply Last reply
      1
      • mrjjM mrjj

        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?
        }

        P Offline
        P Offline
        pantat merah
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved