Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

radio buttons not getting unchecked



  • Hi all,

    I've four draggable line segments on the screen, also I've four radiobuttons to enable which line segment to drag.
    There is a pushbutton to make all the radio buttons unchecked. I do this inside the push button's clicked slot to uncheck all the radio buttons shown above,

    void Program::on_pushButton_one_clicked()
    {
         ui->radioButton_one->setChecked(false);
         ui->radioButton_two->setChecked(false);
         ui->radioButton_three->setChecked(false);
         ui->radioButton_four->setChecked(false);
    }
    

    But none of them actually getting unchecked. I'm not sure if it's because of the QMouseEvent.

    I'm including the minimal version of the code here, can you spot some issues here?

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include<QDialog>
    #include <QPainter>
    #include <QMouseEvent>
    #include <QDebug>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        void paintEvent(QPaintEvent *e);
        int distance(QPoint , QPoint );
    
    
    public slots:
    
            void changeP1value(int);
    
    signals:
    
            void buttonOne(bool);
            void buttonTwo(bool);
    
    
    private:
        Ui::MainWindow *ui;
         bool dragging1 = false; // status var to see if we are dragging
         bool dragging2 = false;
         bool dragging3 = false;
         bool dragging4 = false;
    
             //line points coordinates
             QPoint p11 = QPoint(230,200);
             QPoint p12 = QPoint(280,70);
             QPoint p13 = QPoint(450,70);
             QPoint p14 = QPoint(500,200);
    
             QPoint p21 = QPoint(300,200);
             QPoint p22 = QPoint(320,140);
             QPoint p23 = QPoint(450,140);
             QPoint p24 = QPoint(470,200);
    
             QPoint p31 = QPoint(300,200);
             QPoint p32 = QPoint(320,100);
             QPoint p33 = QPoint(450,100);
             QPoint p34 = QPoint(470,200);
    
             QPoint p41 = QPoint(150,200);
             QPoint p42 = QPoint(170,150);
             QPoint p43 = QPoint(250,150);
             QPoint p44 = QPoint(270,180);
    
             QPoint *CurPoint1=nullptr;
             QPoint *CurPoint2=nullptr;
     
             bool checked1;
             bool checked2;
             bool checked3;
             bool checked4;
    
    protected:
       // we then override / make our own of these function to track mouse movement and clicks
             void mousePressEvent(QMouseEvent *event) ;
             void mouseReleaseEvent(QMouseEvent *event) ;
             void mouseMoveEvent(QMouseEvent *event) ;
    
    private slots:
             void on_pushButton_one_clicked();
             void on_pushButton_two_clicked();
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    // small helper to give us the distance
    int MainWindow::distance(QPoint x1, QPoint x2)
    {
        return abs(x2.y() - x1.y());
    }
    
    
    void MainWindow::paintEvent(QPaintEvent *e)
    {
        
    // to draw the lines 
    QPainter painter1(this);
    
        QPen linepen1(Qt::darkRed);
        linepen1.setWidth(2);
       
        painter1.setPen(linepen1);
        painter1.drawLine(p11,p12);
        painter1.drawLine(p12,p13);
        painter1.drawLine(p13,p14);
    
        painter1.drawLine(p21,p22);
        painter1.drawLine(p22,p23);
        painter1.drawLine(p23,p24);
    
        painter1.drawLine(p31,p32);
        painter1.drawLine(p32,p33);
        painter1.drawLine(p33,p34);
    
        painter1.drawLine(p41,p42);
        painter1.drawLine(p42,p43);
        painter1.drawLine(p43,p44);
    }
    
    // when user clicks
    void MainWindow::mousePressEvent(QMouseEvent *event)
    {
         checked1 = ui->radioButton_one->isChecked();
         checked2 = ui->radioButton_two->isChecked();
         checked3 = ui->radioButton_three->isChecked();
         checked4 = ui->radioButton_four->isChecked();
    
        QPoint mp = event->pos(); // where is mouse
    
    
        // test if we hit the line. give user 10 pixels slack as its hard to hit one pixel
        if (distance ( mp, p12) < 20 && ( mp.x() > p12.x() && mp.x() < p13.x() ) && checked1) {
            dragging1 = true; // flag we are dragging
            CurPoint1 = &p12;
            CurPoint2 = &p13;
        }
        else if (distance ( mp, p22) < 20 && ( mp.x() > p22.x() && mp.x() < p23.x() ) && checked2) {
            dragging2 = true;
            CurPoint1 = &p22;
            CurPoint2 = &p23;
        }
        else if (distance ( mp, p32) < 20 && ( mp.x() > p32.x() && mp.x() < p33.x() ) && checked3) {
            dragging3 = true;
            CurPoint1 = &p32;
            CurPoint2 = &p33;
        }
    
        else if (distance ( mp, p42) < 20 && ( mp.x() > p42.x() && mp.x() < p43.x() ) && checked4) {
            dragging4 = true;
            CurPoint1 = &p42;
            CurPoint2 = &p43;
        }
    
        }
    }
    
    void MainWindow::mouseReleaseEvent(QMouseEvent *event)
    {
        dragging1 = false; // if user release mouse we are not draggign anymore
        dragging2 = false;
        dragging3 = false;
        dragging4 = false;
    
        this->setCursor(QCursor(Qt::CursorShape::ArrowCursor));
    }
    
    // then when mouse move
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
       // If we are dragging, call your normal slider changed function to update your points.
    
    
        if (dragging1 && checked1)
        {
           if(event->y() > 50 && event->y() < 150){
             changeP1value(event->y());
           }
            if(checked1){
               ui->radioButton_one->setText("Segment 3 Val:"+QString::number(CurPoint1->y()+100));
              }
        }
        else if(dragging2 && checked2)
        {
          if(event->y() > 60 && event->y() < 160){
            changeP1value(event->y());
           }
    
          if(checked2){
          ui->radioButton_two->setText("Segment 4 Val:"+QString::number(CurPoint1->y()));
          }
        }
        else if(dragging3 && checked3)
        {
          if(event->y() > 60 && event->y() < 160){
            changeP1value(event->y());
           }
    
          if(checked3){
          ui->radioButton_three->setText("Segment 5 Val:"+QString::number(CurPoint1->y()));
          }
        }
        else if(dragging4 && checked4)
        {
          if(event->y() > 120 && event->y() < 180){
            changeP1value(event->y());
           }
    
          if(checked4){
          ui->radioButton_four->setText("Segment 1 Val:"+QString::number(CurPoint1->y()));
          }
        }
    
        else
           update();
    
    }
    
    void MainWindow::changeP1value(int value)
    {
    
        CurPoint1->setY(value);
        CurPoint2->setY(value);
        update();
    
    }
    
    
    void MainWindow::on_pushButton_one_clicked()
    {
         
    // Here none of the button is getting unchecked  <---------------------------------------
    ui->radioButton_one->setChecked(false);
        ui->radioButton_two->setChecked(false);
        ui->radioButton_three->setChecked(false);
        ui->radioButton_four->setChecked(false);
    
    }
    

    Sorry if it's still too long to check.

    Thanks in advance



  • @viniltc

    I would suggest using the debugger and step through the routines.
    With a break point in on_pushButton_one_clicked, you will see if the routine is called at all. Possibly there is a reset after this, but the best chances for finding the issue shall be with a debugger.


  • Moderators

    @viniltc that is probably the auto exclusive feature that is messing this up

    try the following:

    void Program::on_pushButton_one_clicked()
    {
         ui->radioButton_one->setAutoExclusive(false);
         ui->radioButton_two->setAutoExclusive(false);
         ui->radioButton_three->setAutoExclusive(false);
         ui->radioButton_four->setAutoExclusive(false);
    
         ui->radioButton_one->setChecked(false);
         ui->radioButton_two->setChecked(false);
         ui->radioButton_three->setChecked(false);
         ui->radioButton_four->setChecked(false);
    
         ui->radioButton_one->setAutoExclusive(true);
         ui->radioButton_two->setAutoExclusive(true);
         ui->radioButton_three->setAutoExclusive(true);
         ui->radioButton_four->setAutoExclusive(true);
    }
    


  • @J-Hilk Thanks a lot. It got solved! :)


Log in to reply