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. Sequential qcombobox with qsqlquerymodel
Forum Updated to NodeBB v4.3 + New Features

Sequential qcombobox with qsqlquerymodel

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 678 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.
  • D Offline
    D Offline
    Daniziz
    wrote on last edited by
    #1

    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,

    JonBJ 1 Reply Last reply
    0
    • D Offline
      D Offline
      Daniziz
      wrote on last edited by
      #7

      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.

      1 Reply Last reply
      0
      • D Daniziz

        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,

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

        @Daniziz

        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 to asociacion. 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 same model as 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 = secondComboText neither of those will work....

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Daniziz
          wrote on last edited by
          #3

          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,

          JonBJ 1 Reply Last reply
          0
          • D Daniziz

            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,

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

            @Daniziz

            it makes three iterations on signal "on_SecondCombo_currentIndexChanged".

            Trying to guess: when you change selection in nClienteComboBox it changes the model of nAsociacionComboBox, which is not too bad I guess. But while you are inside on_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 change nAsociacionComboBox's current index and cause re-entry?

            On a separate note: You seem to be leaking a QSqlQueryModel on each callback. I would not expect code to change models here at all. Maybe re-execute a query, but not change the whole model.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Daniziz
              wrote on last edited by
              #5

              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,

              JonBJ 1 Reply Last reply
              0
              • D Daniziz

                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,

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

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

                1 Reply Last reply
                1
                • D Offline
                  D Offline
                  Daniziz
                  wrote on last edited by
                  #7

                  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.

                  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