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. QComboBox binding and returning selected value
Forum Updated to NodeBB v4.3 + New Features

QComboBox binding and returning selected value

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.2k Views 2 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.
  • R Offline
    R Offline
    rhonaldjr
    wrote on last edited by
    #1

    Hi,

    I have two questions (1) How do I bind a return value and text , returned by QSqlQueryModel to a combo box and (2) access the value of the selected item.

    Below is my function to initialize the combobox

    Binding QSqlQueryModel to QComboBox:

    QSqlQuery sql("select id, title from coa_vals where parent_id is null;", db);
    QSqlQueryModel *model = new QSqlQueryModel();
    model->setQuery(sql);
    ui->cbParent->setModel(model);
    ui->cbParent->setModelColumn(1);
    ui->cbParent->show();
    

    Access the selected item's value.

    Below is my code to get the selected value (suppose to be the ID column) from the combo box which doesn't work.

    int row = index <0? ui->cbParent->currentIndex():index;
    QModelIndex idx = ui->cbParent->model()->index(row, 0);
    QVariant data = ui->cbParent->model()->data(idx);
    int catid = data.toInt();
    QString str = "select id, title, details from coa_values where parend_id = " + catid;
    

    This sql always prints as below without the value of ID.

    select id, title, details from coa_values where parent_id =

    What did I miss? I have gone through almost all suggestions found about QComboBox and couldn't work. Alternative is to ditch the model, do manual binding with text and value.

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

      Hi,

      Check the content of data, your toInt conversion might be failing.

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

      1 Reply Last reply
      1
      • R Offline
        R Offline
        rhonaldjr
        wrote on last edited by
        #3

        Yep, made the below change (no idea why I didn't think earlier, could have saved 4 hours of my time) and it worked.

        int row = index <0? ui->cbParent->currentIndex():index;
        QModelIndex idx = ui->cbParent->model()->index(row, 0);
        QVariant data = ui->cbParent->model()->data(idx);
        
        QString str = "select id, title, details from coa_values where parend_id = " + data.toString();
        

        Question: Why does the integer conversion fail? I have noticed this a lot.

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

          I didn't realise one thing, you where trying to add an int to string which will not to what you think/want. So the toInt was likely successful however you string building technique was wrong.

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

          1 Reply Last reply
          1
          • R Offline
            R Offline
            rhonaldjr
            wrote on last edited by
            #5

            Yep, you made me think. the below works. Thanks a lot. Learning to program after years of hiatus. so am a bit rusty I guess.

            QString str = "select id, title, details from coa_values where parend_id = ";
            str.append(QString::number(data.toInt()));
            

            So I guess, I should look to append rather than adding. Is there any other better way?

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

              There's no need for multiple conversion, your toString call was a correct manner to do that.

              Otherwise, you can use QString::arg to build the string.

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

              1 Reply Last reply
              2
              • Karoluss96K Offline
                Karoluss96K Offline
                Karoluss96
                wrote on last edited by
                #7

                @rhonaldjr said in QComboBox binding and returning selected value:

                QSqlQueryModel()

                Hi I've tried the same but in Python. What is cbParent?

                JonBJ 1 Reply Last reply
                0
                • Karoluss96K Karoluss96

                  @rhonaldjr said in QComboBox binding and returning selected value:

                  QSqlQueryModel()

                  Hi I've tried the same but in Python. What is cbParent?

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

                  @Karoluss96 said in QComboBox binding and returning selected value:

                  What is cbParent?

                  A QComboBox created in Designer.

                  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