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. comboBox runtime enable/disable
Qt 6.11 is out! See what's new in the release blog

comboBox runtime enable/disable

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 7 Posters 19.7k Views 3 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.
  • SGaistS SGaist

    @TheCipo76 said in comboBox runtime enable/disable:

    @SGaist
    i'm checking array_Origine[i]..
    depending to it i disable array_Fornitore[i]

    why you tell me that is the same combobox??

    My bad, the code renderer mislead me.

    Anyway, how did you ensure that you are correctly going through that if ?

    TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by TheCipo76
    #10

    @SGaist said in comboBox runtime enable/disable:

    Anyway, how did you ensure that you are correctly going through that if ?

    on pushbutton click i check all (one by one) members of my form with a loop
    when arrive to this "if" i've already checked others widget ( arrays of combobox and lineedit)

    with a dedicated pushbutton i can enable / disable array_Fornitore[i] combobox
    but i wanto to do it without 10 button (array have 10 item) and 10 click!

    when i select (click)"INTERNA" in combobox array_Origine[i] i want disable array_Fornitore[i]
    without any other click and in real time.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #11

      That's clear, what I'm asking is whether you used the debugger or a simple qDebug statement to ensure that your code went through the if that is supposed to disable the other combo box.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      TheCipo76T 1 Reply Last reply
      0
      • SGaistS SGaist

        That's clear, what I'm asking is whether you used the debugger or a simple qDebug statement to ensure that your code went through the if that is supposed to disable the other combo box.

        TheCipo76T Offline
        TheCipo76T Offline
        TheCipo76
        wrote on last edited by
        #12

        @SGaist
        i've check it with a simple QMessageBox inside the if statement:
        my code went throught the if.

        Cobra91151C 1 Reply Last reply
        0
        • TheCipo76T TheCipo76

          @SGaist
          i've check it with a simple QMessageBox inside the if statement:
          my code went throught the if.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #13

          @TheCipo76

          Hi! You can also try to disable/enable the specific item of QComboBox like this:

          Disable:

          qobject_cast<QStandardItemModel*>(comboBox->model())->item(0)->setEnabled(false);
          

          Enable:

          qobject_cast<QStandardItemModel*>(comboBox->model())->item(0)->setEnabled(true);
          

          This code disables/enables the first combobox item.

          Also if you want to disable/enable the combobox, you can simplify it with connects. For example, when you select the value from the 1st combobox (signal activated) then emit your signal to disable the second combobox.

          T 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #14

            Silly question: do you have any logic that re-enables that combo box ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            TheCipo76T 1 Reply Last reply
            0
            • SGaistS SGaist

              Silly question: do you have any logic that re-enables that combo box ?

              TheCipo76T Offline
              TheCipo76T Offline
              TheCipo76
              wrote on last edited by TheCipo76
              #15

              @SGaist
              yes but work only with a pusbutton click

              i think that i have to use the click event for enable / disable in real time

              but combobox is added dinamically in runtime..

              i've tried with:

              connect(array_Fornitore[i], SIGNAL(clicked()), this, SLOT(ATTDIS(int i)));
              

              Dialog .cpp

                  void ATTDIS(int i) {
                  if (array_Origine[i].currentText == "ESTERNA") {
                      array_Fornitore[i]->setDisabled(false);
                  }
                  if (array_Origine[i].currentText == "INTERNA") {
                      array_Fornitore[i]->setDisabled(true);
                  }
              }
              }
              

              dialog .h

              void ATTDIS(int i);
              

              but don't work..
              (array_Origine[i] use of undeclared identifier)
              (array_Fornitore[i] use of undeclared identifier)
              how can i pass array_Orgine[i] and array_Fornitore[i] to the function??

              JonBJ M 2 Replies Last reply
              0
              • TheCipo76T TheCipo76

                @SGaist
                yes but work only with a pusbutton click

                i think that i have to use the click event for enable / disable in real time

                but combobox is added dinamically in runtime..

                i've tried with:

                connect(array_Fornitore[i], SIGNAL(clicked()), this, SLOT(ATTDIS(int i)));
                

                Dialog .cpp

                    void ATTDIS(int i) {
                    if (array_Origine[i].currentText == "ESTERNA") {
                        array_Fornitore[i]->setDisabled(false);
                    }
                    if (array_Origine[i].currentText == "INTERNA") {
                        array_Fornitore[i]->setDisabled(true);
                    }
                }
                }
                

                dialog .h

                void ATTDIS(int i);
                

                but don't work..
                (array_Origine[i] use of undeclared identifier)
                (array_Fornitore[i] use of undeclared identifier)
                how can i pass array_Orgine[i] and array_Fornitore[i] to the function??

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #16

                @TheCipo76
                No offence intended, but from the same sort of thing in your previous posts you might like to read up on variable scope. And if you do understand that for normal variables, exactly the same applies when they are arrays.

                Rather than worrying about how to pass these arrays as parameters each time to slots and other individual functions, wouldn't your code logic be simpler if they were defined as member variables of your class (the dialog)?

                TheCipo76T 1 Reply Last reply
                2
                • JonBJ JonB

                  @TheCipo76
                  No offence intended, but from the same sort of thing in your previous posts you might like to read up on variable scope. And if you do understand that for normal variables, exactly the same applies when they are arrays.

                  Rather than worrying about how to pass these arrays as parameters each time to slots and other individual functions, wouldn't your code logic be simpler if they were defined as member variables of your class (the dialog)?

                  TheCipo76T Offline
                  TheCipo76T Offline
                  TheCipo76
                  wrote on last edited by TheCipo76
                  #17

                  @JonB
                  this is my dialog .h:

                  class inserisciarticolo : public QDialog
                  {
                      Q_OBJECT
                  
                  public:
                      explicit inserisciarticolo(QWidget *parent = nullptr);
                      ~inserisciarticolo();
                  
                  
                  private slots:
                  
                      void on_pushButton_Annulla_clicked();
                  
                      void on_pushButton_Inserisci_clicked();
                  
                      //void ATTDIS(int i);
                  
                  private:
                      Ui::inserisciarticolo *ui;
                  
                      QSqlDatabase aDatabase;
                      QSqlQuery *aModel;
                  
                      //Array Componenti
                      QLineEdit *array_Codice[10];
                      QLineEdit *array_Descrizione[10];
                      QSpinBox *array_Qta[10];
                      QComboBox *array_Origine[10];  //<--------------------
                      QComboBox *array_Fornitore[10]; //<------------------
                  
                  1 Reply Last reply
                  0
                  • TheCipo76T TheCipo76

                    @SGaist
                    yes but work only with a pusbutton click

                    i think that i have to use the click event for enable / disable in real time

                    but combobox is added dinamically in runtime..

                    i've tried with:

                    connect(array_Fornitore[i], SIGNAL(clicked()), this, SLOT(ATTDIS(int i)));
                    

                    Dialog .cpp

                        void ATTDIS(int i) {
                        if (array_Origine[i].currentText == "ESTERNA") {
                            array_Fornitore[i]->setDisabled(false);
                        }
                        if (array_Origine[i].currentText == "INTERNA") {
                            array_Fornitore[i]->setDisabled(true);
                        }
                    }
                    }
                    

                    dialog .h

                    void ATTDIS(int i);
                    

                    but don't work..
                    (array_Origine[i] use of undeclared identifier)
                    (array_Fornitore[i] use of undeclared identifier)
                    how can i pass array_Orgine[i] and array_Fornitore[i] to the function??

                    M Offline
                    M Offline
                    mpergand
                    wrote on last edited by
                    #18

                    @TheCipo76 said in comboBox runtime enable/disable:

                    connect(array_Fornitore[i], SIGNAL(clicked()), this, SLOT(ATTDIS(int i)));

                    You need to capture the var i in a lambda:

                    connect(array_Fornitore[i], &QComboBox::clicked, [i]() 
                    { 
                    ATTDIS(i);
                    });
                    
                    TheCipo76T 2 Replies Last reply
                    2
                    • M mpergand

                      @TheCipo76 said in comboBox runtime enable/disable:

                      connect(array_Fornitore[i], SIGNAL(clicked()), this, SLOT(ATTDIS(int i)));

                      You need to capture the var i in a lambda:

                      connect(array_Fornitore[i], &QComboBox::clicked, [i]() 
                      { 
                      ATTDIS(i);
                      });
                      
                      TheCipo76T Offline
                      TheCipo76T Offline
                      TheCipo76
                      wrote on last edited by
                      #19

                      @mpergand
                      it's very interesting..

                      i can capture i var and pass it to a slot function
                      if i have understand..

                      but once i have done it what i have to do??
                      slot function do what i need?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mpergand
                        wrote on last edited by
                        #20

                        The lambda function becomes the slot function.
                        You probably need to capture 'this' as well to get access to methods and vars of the class instance.

                        TheCipo76T 1 Reply Last reply
                        2
                        • M mpergand

                          The lambda function becomes the slot function.
                          You probably need to capture 'this' as well to get access to methods and vars of the class instance.

                          TheCipo76T Offline
                          TheCipo76T Offline
                          TheCipo76
                          wrote on last edited by TheCipo76
                          #21

                          @mpergand
                          thank you very much for your reply
                          but i don't think i'm able to do this..

                          now i try to learn about lamba expression

                          1 Reply Last reply
                          1
                          • TheCipo76T Offline
                            TheCipo76T Offline
                            TheCipo76
                            wrote on last edited by TheCipo76
                            #22
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • M mpergand

                              @TheCipo76 said in comboBox runtime enable/disable:

                              connect(array_Fornitore[i], SIGNAL(clicked()), this, SLOT(ATTDIS(int i)));

                              You need to capture the var i in a lambda:

                              connect(array_Fornitore[i], &QComboBox::clicked, [i]() 
                              { 
                              ATTDIS(i);
                              });
                              
                              TheCipo76T Offline
                              TheCipo76T Offline
                              TheCipo76
                              wrote on last edited by TheCipo76
                              #23

                              @mpergand

                              i've tried with this code:

                              connect(array_Origine[i], SIGNAL(currentTextChanged(QString)) [i] () { ATTDIS(i);} );
                              

                              and with this:

                              connect(array_Origine[i], SIGNAL(currentTextChanged(QString)) [this, i] () { ATTDIS(i);} );
                              

                              but give me this error:

                              called object type 'char' is not a function or function pointer

                              how can i solve it??

                              void inserisciarticolo::ATTDIS(int i)
                              {
                                  if (array_Origine[i]->currentText() == "ESTERNA") {
                                      array_Fornitore[i]->setDisabled(false);
                                      array_Fornitore[i]->repaint();
                                  }
                                  if (array_Origine[i]->currentText() == "INTERNA") {
                                      array_Fornitore[i]->setDisabled(true);
                                      array_Fornitore[i]->repaint();
                                  }
                              
                              }
                              
                              M 1 Reply Last reply
                              0
                              • TheCipo76T TheCipo76

                                @mpergand

                                i've tried with this code:

                                connect(array_Origine[i], SIGNAL(currentTextChanged(QString)) [i] () { ATTDIS(i);} );
                                

                                and with this:

                                connect(array_Origine[i], SIGNAL(currentTextChanged(QString)) [this, i] () { ATTDIS(i);} );
                                

                                but give me this error:

                                called object type 'char' is not a function or function pointer

                                how can i solve it??

                                void inserisciarticolo::ATTDIS(int i)
                                {
                                    if (array_Origine[i]->currentText() == "ESTERNA") {
                                        array_Fornitore[i]->setDisabled(false);
                                        array_Fornitore[i]->repaint();
                                    }
                                    if (array_Origine[i]->currentText() == "INTERNA") {
                                        array_Fornitore[i]->setDisabled(true);
                                        array_Fornitore[i]->repaint();
                                    }
                                
                                }
                                
                                M Offline
                                M Offline
                                mpergand
                                wrote on last edited by
                                #24

                                @TheCipo76 said in comboBox runtime enable/disable:

                                connect(array_Origine[i], SIGNAL(currentTextChanged(QString)) [this, i] () { ATTDIS(i);} );

                                connect(array_Origine[i], &QComboBox::currentTextChanged) [this, i] (QString str) { ATTDIS(i);} );

                                TheCipo76T 1 Reply Last reply
                                0
                                • M mpergand

                                  @TheCipo76 said in comboBox runtime enable/disable:

                                  connect(array_Origine[i], SIGNAL(currentTextChanged(QString)) [this, i] () { ATTDIS(i);} );

                                  connect(array_Origine[i], &QComboBox::currentTextChanged) [this, i] (QString str) { ATTDIS(i);} );

                                  TheCipo76T Offline
                                  TheCipo76T Offline
                                  TheCipo76
                                  wrote on last edited by TheCipo76
                                  #25

                                  @mpergand said in comboBox runtime enable/disable:

                                  connect(array_Origine[i], &QComboBox::currentTextChanged) [this, i] (QString str) { ATTDIS(i);} );

                                  don't work..

                                  'QString' does not refer to a value

                                  error: no matching member function for call to 'connect'

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mpergand
                                    wrote on last edited by
                                    #26
                                    QComboBox box;
                                    box.setEditable(true);
                                    QObject::connect(&box,&QComboBox::currentTextChanged,[](QString str)
                                    {
                                    qDebug()<<str;
                                    });
                                    
                                    box.show();
                                    

                                    works as expected ....

                                    1 Reply Last reply
                                    1
                                    • M Offline
                                      M Offline
                                      mpergand
                                      wrote on last edited by mpergand
                                      #27

                                      sorry typo:
                                      connect(array_Origine[i], &QComboBox::currentTextChanged ) <- remove )
                                      [this, i] (QString str) { ATTDIS(i);} );

                                      TheCipo76T 1 Reply Last reply
                                      1
                                      • M mpergand

                                        sorry typo:
                                        connect(array_Origine[i], &QComboBox::currentTextChanged ) <- remove )
                                        [this, i] (QString str) { ATTDIS(i);} );

                                        TheCipo76T Offline
                                        TheCipo76T Offline
                                        TheCipo76
                                        wrote on last edited by TheCipo76
                                        #28

                                        Finally works!

                                        i passed no parameters to the function ATTDIS():

                                        call the function then
                                        i check with a loop all members of array (with "for" i index) and when
                                        find "ESTERNA" enable comboFornitore[i]
                                        when find "INTERNA" disable comboFornitore[i]

                                        Thanks to all for patience and help gived

                                        1 Reply Last reply
                                        0
                                        • Cobra91151C Cobra91151

                                          @TheCipo76

                                          Hi! You can also try to disable/enable the specific item of QComboBox like this:

                                          Disable:

                                          qobject_cast<QStandardItemModel*>(comboBox->model())->item(0)->setEnabled(false);
                                          

                                          Enable:

                                          qobject_cast<QStandardItemModel*>(comboBox->model())->item(0)->setEnabled(true);
                                          

                                          This code disables/enables the first combobox item.

                                          Also if you want to disable/enable the combobox, you can simplify it with connects. For example, when you select the value from the 1st combobox (signal activated) then emit your signal to disable the second combobox.

                                          T Offline
                                          T Offline
                                          TruongNguyen
                                          wrote on last edited by
                                          #29

                                          @Cobra91151 Thank you. It worked.

                                          1 Reply Last reply
                                          0

                                          • Login

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