Solved comboBox runtime enable/disable
-
Hi,
i have 2 ComboBoxwhen i select a value in 1st ComboBox
i have to enable or disable the 2nd ComboBoxi try with:
ComboBox->setEnabled(true)
ComboBox->setDisabled (true)but don't work..
i read about this problem in this post:
https://forum.qt.io/topic/37207/setenabled-vs-setdisabled/11
but i don't know how to do it.
Anyone can help me please? -
@TheCipo76 said in comboBox runtime enable/disable:
i try with:
Can you show me the whole slot code?
-
i've created an array of 10 ComboBox populated from DB
with this code:q.prepare("Select RAGSOC from FORNITORI order by RAGSOC"); for (int i = 0; i < 10; i++) { array_Fornitore[i] = new QComboBox; ui->gridLayout_5->addWidget(array_Fornitore[i]); array_Fornitore[i]->setFixedWidth(300); array_Fornitore[i]->setFixedHeight(30); //Caricamento Combobox FORNITORE DA DATABASE if (q.exec()) { QSqlQueryModel *tModel = new QSqlQueryModel(this); tModel->setQuery(q); array_Fornitore[i]->setModel(tModel); } array_Fornitore[i]->setDisabled(true); }
then i try to enable with this code (on pushbutton click):
if (array_Origine[i]->currentText() == "INTERNA"){ array_Fornitore[i]->setEnabled(true); //array_Fornitore[i]->clear(); return; }
-
@TheCipo76 said in comboBox runtime enable/disable:
array_Fornitore[i]->setEnabled(true);
can you just try with
setDisabled(false)
? -
Hi,
You are enabling the same combo box that you are checking.
-
@Ratzz
yes i've already try: don't work -
@SGaist
i'm checking array_Origine[i]..
depending to it i disable array_Fornitore[i]why you tell me that is the same combobox??
-
@TheCipo76
i have to do this without pushbutton..i read something about QEvent..
is the right and only way?
-
@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 ?
-
@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. -
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. -
@SGaist
i've check it with a simple QMessageBox inside the if statement:
my code went throught the if. -
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.
-
Silly question: do you have any logic that re-enables that combo box ?
-
@SGaist
yes but work only with a pusbutton clicki 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?? -
@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)?
-
@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]; //<------------------
-
@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); });
-
@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? -
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.