How to use setDisabled with parameterised Constructor..?



  • Hi,

    I want to use the SetDisabled with a parameterised constructor, let me explain with a piece of code

    MainDialog.h

    public MainDialog : QWidget
    {
    public:
            MainDialog(QWidget *parent=0);
    private:
    QPushButton ok_button;
    
    private slots:
    void ok_slot();
    };
    

    MainDialog.cpp

    MainDialog::MainDialog(QWidget *parent):QWidget(parent)
    {
       ok_button = new QPushButton("OK");
    
    connect(ok_button,SIGNAL(clicked()),this,SLOT(ok_slot()));
    
    }
    void MainDialog::ok_slot()
    {
       MacDlg *dlg = new MacDlg(this);
       dlg->show();
    }
    

    MacDialog.h

    class MainDialog;
    
    class MacDialog : public QWidget
    {
            Q_OBJECT
    
            public:
                    MacDialog(MainDialog *);
                    MainDialog *Tdlg;
    private:
    QPushButton* b1Button;
    
    public slots:
                    int button1();
    };
    

    MacDialog.cpp

    MacDialog::MacDialog(MainDialog *Cdlg)//If i am writing code in this way then the contents are not disabling
    MacDialog::MacDialog()//If i am writing code in this way then the contents are disabling
    {
    Tdlg=Cdlg;
    
    b1Button =new QPushButton(this);
            b1Button->setText("Disable");
    connect(b1Button , SIGNAL(clicked()), this, SLOT(button1()));
    }
    
    int MacDialog::button1()
    {
     this->setDisabled(true);
    }
    

    I am not getting where is the problem, i hope i have explained in a way to understand.

    Please help me.

    Thanks in advance,
    Rohith.G



  • Hello Rohith;

    I don't think that the problem is from parametrised Constructor,

    I tested your code And everything works as expected,

    here is your code again:

    MacDialog.h

    #ifndef MACDIALOG_H
    #define MACDIALOG_H
    
    #include <QWidget>
    #include <QPushButton>
    
    class MainDialog;
    
    class MacDialog : public QWidget
    {
        Q_OBJECT
    public:
        MacDialog(MainDialog *Cdlg);
    private slots:
        int button1();
    private:
        QPushButton* b1Button;
        MainDialog *Tdlg;
    };
    
    #endif // MACDIALOG_H
    

    MacDialog.cpp

    #include "macdialog.h"
    
    MacDialog::MacDialog(MainDialog *Cdlg)
    {
        Tdlg=Cdlg;
    
        b1Button =new QPushButton(this);
                b1Button->setText("Disable");
                connect(b1Button , SIGNAL(clicked()), this, SLOT(button1()));
    }
    
    int MacDialog::button1()
    {
        this->setDisabled(true);
    }
    

    MainDialog.h

    #ifndef MAINDIALOG_H
    #define MAINDIALOG_H
    
    #include <QWidget>
    #include <QPushButton>
    
    class MainDialog : public QWidget
    {
        Q_OBJECT
    public:
        explicit MainDialog(QWidget *parent = 0);
    
    signals:
    
    private slots:
        void ok_slot();
    private:
        QPushButton* ok_button;
    };
    
    
    
    
    #endif // MAINDIALOG_H
    

    MainDialog.cpp

    #include "maindialog.h"
    #include "macdialog.h"
    
    #include <QHBoxLayout>
    
    MainDialog::MainDialog(QWidget *parent) : QWidget(parent)
    {
        ok_button = new QPushButton("OK");
    
        QHBoxLayout* l = new QHBoxLayout(this);
    
        this->setLayout(l);
        l->addWidget(ok_button);
        connect(ok_button,SIGNAL(clicked()),this,SLOT(ok_slot()));
    }
    
    void MainDialog::ok_slot()
    {
        MacDialog *dlg = new MacDialog(this);
        dlg->show();
    }
    

    and here is the result when disable button is clicked

    alt text


  • Moderators

    @Rohith

    Where do you have your "setDisabled" in a constructor?

    MacDialog::MacDialog(MainDialog *Cdlg)//If i am writing code in this way then the contents are not disabling
    MacDialog::MacDialog()//If i am writing code in this way then the contents are disabling
    {
    Tdlg=Cdlg;
    
    b1Button =new QPushButton(this);
            b1Button->setText("Disable");
    connect(b1Button , SIGNAL(clicked()), this, SLOT(button1()));
    }
    
    int MacDialog::button1()
    {
     this->setDisabled(true);
    }
    

    The code above has a setDisabled in button1 function, but that is not called in the contructor. It will be executed when clicked on the button.

            b1Button->setText("Disable");
    

    Simply sets the text "Disable" in the button.

    If you like to disable MacDialog right away when constructing, you can try calling the slot "button1()" in the constructor.
    The slot should be "void button1()".



  • @mostefa

    Hi mostefa thanks for cross verifying my code and for replying me.The problem that i am facing at my side is when i am using constructor in the below fashion

    Machine_dlg::Machine_dlg(MainDialog *Cdlg)

    I am able to observe the change when i minimize and maximize my UI then i am able to observe the change i.e the contents are getting disabled.Where as, if i use a constructor in the below fashion

    Machine_dlg::Machine_dlg()

    I am able to observe the change immediately i.e with out minimize and maximize i am able to see that the contents are getting disabled.

    Sorry if my question bothers you, but i am really facing problem

    Thanks in advance,
    Rohith.G


  • Moderators

    @Rohith What are the differences between Machine_dlg::Machine_dlg(MainDialog *Cdlg) and Machine_dlg::Machine_dlg(), I mean what do you do in these constructors ?
    Do you pass a pointer to Machine_dlg::Machine_dlg(MainDialog *Cdlg) ?
    Here you do not disable the button, so I'm wondering why you expect it to be disabled (this was already pointed out by @koahnig, but you didn't reply to him):

    MainDialog::MainDialog(QWidget *parent) : QWidget(parent)
    {
        ok_button = new QPushButton("OK");
    
        QHBoxLayout* l = new QHBoxLayout(this);
    
        this->setLayout(l);
        l->addWidget(ok_button);
        connect(ok_button,SIGNAL(clicked()),this,SLOT(ok_slot()));
    }
    


  • @koahnig

    Thanks for replying

    int MacDialog::button1()
    {
     this->setDisabled(true);
    process();//After disabling the MacDialog i want to execute some other statements that are present in the process function till then i want to stop the user from interacting with the UI and after completing execution i want to re-enable the UI
    }
    

    And the MacDialog contains multiple buttons and slots.For easy understanding i have trimmed my code.

    Thanks in advance,
    Rohith.G



  • @Rohith

    Could you give us the whole content of the two constructors :

    Machine_dlg::Machine_dlg(MainDialog *Cdlg)
    {
    // Yout content for this contructor
    }

    and

    Machine_dlg::Machine_dlg()
    {
    // your content for this constructor
    }

    ?



  • @mostefa

    Hi mostefa thanks for replying as you people said there is no such problem with constructor.In the code itself there is a eventLoop running in the problem is with my code,sorry for confusing you people and the issue got resolved.



  • Hi @Rohith

    Happy to see that your problem is solved now,

    you are welcome !


Log in to reply
 

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