Solved comboBox runtime enable/disable
-
@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. -
@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
-
This post is deleted! -
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(); } }
-
@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);} );
-
@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'
-
QComboBox box; box.setEditable(true); QObject::connect(&box,&QComboBox::currentTextChanged,[](QString str) { qDebug()<<str; }); box.show();
works as expected ....
-
sorry typo:
connect(array_Origine[i], &QComboBox::currentTextChanged ) <- remove )
[this, i] (QString str) { ATTDIS(i);} ); -
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
-
@Cobra91151 Thank you. It worked.