Sequential qcombobox with qsqlquerymodel
-
Hi all,
I have an app with 3 qComboBox i would like to be linked.
My first combo is "hardcoded" (it is a yes/no combo).
ui->FirstCombo->addItem("YES"); ui->FirstCombo->addItem("NO");I want my second combo as a result of a query using data from first combo like this:
void MainWindow::on_firstCombo_currentIndexChanged(const QString &firstCombotext) { QString qry = "SELECT fieldA FROM tableA WHERE fieldB = firstCombotext" model->setQuery(qry); ui->SecondCombo->setModel(asociacion); }This works fine, my problem is with my third combo, which uses data from first and second combo when user selects one value on second combo:
void MainWindow::on_SecondCombo_currentIndexChanged(const QString &secondComboText) { QString qry = "SELECT fieldA FROM tableB WHERE fieldB = secondComboText AND fieldC ="+ ui->FirstCombo->currenttext(); model->setQuery(qry); ui->ThirdCombo->setModel(model); }Here I screw me my combos, and If I debug it, with every change I do on first combo, it makes three iterations on signal "on_SecondCombo_currentIndexChanged".
Is there any way to do it?
Regards,
-
Hi @JonB
Thats it... im sooooooo dumb.
I was pointing to the same combo, now it works!!
I've changed
ui->nAsociacionComboBox->setModel(direcciones);to
ui->nDireccionesComboBox->setModel(direcciones); //Third combo!Now it's working fine!!!
Thank you very much.
-
Hi all,
I have an app with 3 qComboBox i would like to be linked.
My first combo is "hardcoded" (it is a yes/no combo).
ui->FirstCombo->addItem("YES"); ui->FirstCombo->addItem("NO");I want my second combo as a result of a query using data from first combo like this:
void MainWindow::on_firstCombo_currentIndexChanged(const QString &firstCombotext) { QString qry = "SELECT fieldA FROM tableA WHERE fieldB = firstCombotext" model->setQuery(qry); ui->SecondCombo->setModel(asociacion); }This works fine, my problem is with my third combo, which uses data from first and second combo when user selects one value on second combo:
void MainWindow::on_SecondCombo_currentIndexChanged(const QString &secondComboText) { QString qry = "SELECT fieldA FROM tableB WHERE fieldB = secondComboText AND fieldC ="+ ui->FirstCombo->currenttext(); model->setQuery(qry); ui->ThirdCombo->setModel(model); }Here I screw me my combos, and If I debug it, with every change I do on first combo, it makes three iterations on signal "on_SecondCombo_currentIndexChanged".
Is there any way to do it?
Regards,
Here I screw me my combos
That sounds bad :)
Goodness knows what your behaviour is. In combo #2:
model->setQuery(qry); ui->SecondCombo->setModel(asociacion);You set
model's query, but you set the combo's model toasociacion. Doesn't look right.In combo #3:
model->setQuery(qry); ui->ThirdCombo->setModel(model);Here you do set the combo's model to
model. But is that the samemodelas you are using for combo #2?Not sure how much of the code you show is your actual code.... If you are really using literally
WHERE fieldB = firstCombotext,WHERE fieldB = secondComboTextneither of those will work.... -
Hi @JonB
I was trying to post as pseudocode to be more clear... bad idea... my real code is this one:
ui->nClienteComboBox->addItem("YES"); ui->nClienteComboBox->addItem("NO"); void MainWindow::on_nClienteComboBox_currentIndexChanged(const QString &arg1) { //Modelo SQL para obtener los compos de base de datos QSqlQuery qry_navision(QSqlDatabase::database("navision")); QSqlQueryModel *asociacion = new QSqlQueryModel(); QString qry = "SELECT DISTINCT [Nombre Comercial] FROM Direcciones WHERE Cliente = (SELECT Contador FROM Clientes WHERE [Nombre Fiscal] = '"+arg1+"') "; //Cargamos el combo proyectos de BBDD qry_navision.prepare(qry); asociacion->setQuery(qry_navision); ui->nAsociacionComboBox->setModel(asociacion); } void MainWindow::on_nAsociacionComboBox_currentIndexChanged(const QString &arg2) { QSqlQuery qry_navision1(QSqlDatabase::database("navision")); QSqlQueryModel *direcciones = new QSqlQueryModel(); QString qry = "SELECT DISTINCT Domicilio FROM Direcciones WHERE " "Cliente = (SELECT Contador FROM Clientes WHERE [Nombre Fiscal] = '" +ui->nClienteComboBox->currentText()+"') AND [Nombre Comercial] = '" +arg2+"'"; //Cargamos el combo proyectos de BBDD qry_navision1.prepare(qry); direcciones->setQuery(qry_navision1); ui->nAsociacionComboBox->setModel(direcciones); }As you can see, i dont use same var names for models, and SQL syntax is correct... (I've launched the queries on the database ok).
Sorry for the previous one :)
Regards,
-
Hi @JonB
I was trying to post as pseudocode to be more clear... bad idea... my real code is this one:
ui->nClienteComboBox->addItem("YES"); ui->nClienteComboBox->addItem("NO"); void MainWindow::on_nClienteComboBox_currentIndexChanged(const QString &arg1) { //Modelo SQL para obtener los compos de base de datos QSqlQuery qry_navision(QSqlDatabase::database("navision")); QSqlQueryModel *asociacion = new QSqlQueryModel(); QString qry = "SELECT DISTINCT [Nombre Comercial] FROM Direcciones WHERE Cliente = (SELECT Contador FROM Clientes WHERE [Nombre Fiscal] = '"+arg1+"') "; //Cargamos el combo proyectos de BBDD qry_navision.prepare(qry); asociacion->setQuery(qry_navision); ui->nAsociacionComboBox->setModel(asociacion); } void MainWindow::on_nAsociacionComboBox_currentIndexChanged(const QString &arg2) { QSqlQuery qry_navision1(QSqlDatabase::database("navision")); QSqlQueryModel *direcciones = new QSqlQueryModel(); QString qry = "SELECT DISTINCT Domicilio FROM Direcciones WHERE " "Cliente = (SELECT Contador FROM Clientes WHERE [Nombre Fiscal] = '" +ui->nClienteComboBox->currentText()+"') AND [Nombre Comercial] = '" +arg2+"'"; //Cargamos el combo proyectos de BBDD qry_navision1.prepare(qry); direcciones->setQuery(qry_navision1); ui->nAsociacionComboBox->setModel(direcciones); }As you can see, i dont use same var names for models, and SQL syntax is correct... (I've launched the queries on the database ok).
Sorry for the previous one :)
Regards,
it makes three iterations on signal "on_SecondCombo_currentIndexChanged".
Trying to guess: when you change selection in
nClienteComboBoxit changes the model ofnAsociacionComboBox, which is not too bad I guess. But while you are insideon_nAsociacionComboBox_currentIndexChanged()you change the model it itself is currently using (I don't know what you're trying to achieve). I don't think that's a good idea at all.... This could well be causing your "cascading" calls, e.g. among other things, won't this likely changenAsociacionComboBox's current index and cause re-entry?On a separate note: You seem to be leaking a
QSqlQueryModelon each callback. I would not expect code to change models here at all. Maybe re-execute a query, but not change the whole model. -
Hi @JonB,
I want my first combo to act like a filter for the values of the second one.
Then, when you select a value from the second, it acts like a filter for the third one, but combined with the value of the first combo.
- User select YES on First combo.
- The results on the second are halved due to first one. User selects XXXX
- The list of the third combo is filtered by YES and XXX.
How can I do that?
Regards,
-
Hi @JonB,
I want my first combo to act like a filter for the values of the second one.
Then, when you select a value from the second, it acts like a filter for the third one, but combined with the value of the first combo.
- User select YES on First combo.
- The results on the second are halved due to first one. User selects XXXX
- The list of the third combo is filtered by YES and XXX.
How can I do that?
Regards,
@Daniziz
Well you would do that just like you've written in your 3 bullet points. Something like:- Selecting in combo #1 causes both combo #2 & combo #3 values to be recalculated. (Or, you may be saying you don't want #3 to be re-evaluated immediately from #1, you want to wait for user to pick in #2 too, I don't know.)
- Selecting in combo #2 causes just combo #3 values to be recalculated (based in what has just been picked in #2 plus whatever is currently picked in #1).
I think that's what you're saying you want.
But you have:
void MainWindow::on_nAsociacionComboBox_currentIndexChanged(const QString &arg2) ... ui->nAsociacionComboBox->setModel(direcciones);so you are making choosing in combo #2 actually re-evaluate/change what is in combo #2, and from inside combo #2's own slot. That does not sound right, and could well cause re-entry.
-
Hi @JonB
Thats it... im sooooooo dumb.
I was pointing to the same combo, now it works!!
I've changed
ui->nAsociacionComboBox->setModel(direcciones);to
ui->nDireccionesComboBox->setModel(direcciones); //Third combo!Now it's working fine!!!
Thank you very much.