Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use setDisabled with parameterised Constructor..?
QtWS25 Last Chance

How to use setDisabled with parameterised Constructor..?

Scheduled Pinned Locked Moved Solved General and Desktop
thisdisableconstructorpara
9 Posts 4 Posters 2.5k Views
  • 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.
  • R Offline
    R Offline
    Rohith
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • R Rohith

        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

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @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()".

        Vote the answer(s) that helped you to solve your issue(s)

        R 1 Reply Last reply
        0
        • M mostefa

          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

          R Offline
          R Offline
          Rohith
          wrote on last edited by
          #4

          @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

          jsulmJ 1 Reply Last reply
          0
          • R Rohith

            @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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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()));
            }
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • K koahnig

              @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()".

              R Offline
              R Offline
              Rohith
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mostefa
                wrote on last edited by mostefa
                #7

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

                ?

                R 1 Reply Last reply
                0
                • M mostefa

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

                  ?

                  R Offline
                  R Offline
                  Rohith
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mostefa
                    wrote on last edited by
                    #9

                    Hi @Rohith

                    Happy to see that your problem is solved now,

                    you are welcome !

                    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