Results 1 to 4 of 4

Thread: field() method of QWizard returning currentIndex of combo-boxes, not values. Help

  1. #1
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy field() method of QWizard returning currentIndex of combo-boxes, not values. Help

    Hi!

    As the title suggests, I'm attempting to get the selected value from a QComboBox on a wizard page. I have a bunch of combo-boxes which are populated with data from a relational model's relations. I was struggling trying to insert a record into the database with the selected values and have discovered that calling value() is returning the currentIndex of the combo-boxes, rather than the selected value.

    I should point out here that I'm both very new to Qt and C++, so go easy on me.

    My code thus far:

    The Patient object is derived from QSqlRelationalTableModel. I have a bunch of methods in that class to return the relations of the model, hence the "patient->gender()" type calls. They return a object of type QSqlRelation.

    Qt Code:
    1. /***
    2.  * Register all of the necessary fields with this
    3.  * wizard form page.
    4.  */
    5. void NewPatientPageTwo::registerFields()
    6. {
    7. registerField("doctor", ui->doctor);
    8. registerField("gender", ui->gender);
    9. registerField("sexual_orientation", ui->sexual_orientation);
    10. registerField("marital_status", ui->marital_status);
    11. registerField("employment_status", ui->employment_status);
    12. registerField("performance_status", ui->performance_status);
    13. registerField("alcohol_consumption", ui->alcohol_consumption);
    14. registerField("clinical_group", ui->clinical_group);
    15. registerField("ethnic_category", ui->ethnic_category);
    16. }
    17.  
    18. /***
    19.  * Each of the fields on this wizard page are
    20.  * combo-boxes that need to be populated with
    21.  * information held in the database.
    22.  */
    23. void NewPatientPageTwo::populateFields()
    24. {
    25. Patient *patientModel = new Patient(this);
    26.  
    27. // doctor
    28. ui->doctor->setModel(patientModel->doctor());
    29. ui->doctor->setModelColumn(patientModel->doctor()->fieldIndex("lastname"));
    30.  
    31. // gender
    32. ui->gender->setModel(patientModel->gender());
    33. ui->gender->setModelColumn(patientModel->gender()->fieldIndex("description"));
    34.  
    35. // sexual orientation
    36. ui->sexual_orientation->setModel(patientModel->sexualOrientation());
    37. ui->sexual_orientation->setModelColumn(patientModel->sexualOrientation()->fieldIndex("description"));
    38.  
    39. // marital status
    40. ui->marital_status->setModel(patientModel->maritalStatus());
    41. ui->marital_status->setModelColumn(patientModel->maritalStatus()->fieldIndex("description"));
    42.  
    43. // employment status
    44. ui->employment_status->setModel(patientModel->employmentStatus());
    45. ui->employment_status->setModelColumn(patientModel->employmentStatus()->fieldIndex("description"));
    46.  
    47. // performance status
    48. ui->performance_status->setModel(patientModel->performanceStatus());
    49. ui->performance_status->setModelColumn(patientModel->performanceStatus()->fieldIndex("description"));
    50.  
    51. // alcohol consumption
    52. ui->alcohol_consumption->setModel(patientModel->alcoholStatus());
    53. ui->alcohol_consumption->setModelColumn(patientModel->alcoholStatus()->fieldIndex("description"));
    54.  
    55. // clinical group
    56. ui->clinical_group->setModel(patientModel->clinicalGroup());
    57. ui->clinical_group->setModelColumn(patientModel->clinicalGroup()->fieldIndex("description"));
    58.  
    59. // ethnic category
    60. ui->ethnic_category->setModel(patientModel->ethnicCategory());
    61. ui->ethnic_category->setModelColumn(patientModel->ethnicCategory()->fieldIndex("description"));
    62. }
    To copy to clipboard, switch view to plain text mode 

    This is the imlpementation of the accept method in the wizard class. It's called when I hit the finish button.
    Qt Code:
    1. void NewPatient::accept()
    2. {
    3. Patient *model = new Patient(this);
    4. unsigned int row = model->rowCount();
    5.  
    6. if (model->insertRow(row)) {
    7. model->setData(model->index(row, Patient::NHSNumber), field("nhsnumber"));
    8. model->setData(model->index(row, Patient::EmploymentStatus), field("employment_status"));
    9. model->setData(model->index(row, Patient::Gender), field("gender"));
    10. model->setData(model->index(row, Patient::MaritalStatus), field("marital_status"));
    11. model->setData(model->index(row, Patient::PerformanceStatus), field("performance_status"));
    12. model->setData(model->index(row, Patient::SexualOrientation), field("sexual_orientation"));
    13. model->setData(model->index(row, Patient::ClinicalGroup), field("clinical_group"));
    14. model->setData(model->index(row, Patient::EthnicCategory), field("ethnic_category"));
    15. model->setData(model->index(row, Patient::AlcoholStatus), field("alcohol_consumption"));
    16. model->setData(model->index(row, Patient::Doctor), field("doctor"));
    17. model->setData(model->index(row, Patient::Salutation), field("title"));
    18. model->setData(model->index(row, Patient::BirthDate), field("dob").toDate().toString(Qt::ISODate));
    19. model->setData(model->index(row, Patient::FirstName), field("firstname"));
    20. model->setData(model->index(row, Patient::LastName), field("lastname"));
    21. model->setData(model->index(row, Patient::RegisteredDate), QDate::currentDate().toString(Qt::ISODate));
    22.  
    23. model->database().transaction();
    24.  
    25. if (model->submitAll()) {
    26. model->database().commit();
    27. } else {
    28. model->database().rollback();
    29. }
    30. }
    31.  
    32. close();
    33. }
    To copy to clipboard, switch view to plain text mode 

    The insert is failing because the calls to field("inputname") aren't returning the value, as expected. They're returning the currentIndex. Any help would be really appreciated.

    Thanks!

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: field() method of QWizard returning currentIndex of combo-boxes, not values. Help

    Disclaimer: I have never used the QWizard or the QWizardPage classes but your post piqued my curiosity. So I took a look at the docs. Check this out: setDefaultProperty.

    QComboBox:getText() might work too:
    Qt Code:
    1. model->setData(model->index(row, Patient::MaritalStatus), ui->marital_status->getText());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: field() method of QWizard returning currentIndex of combo-boxes, not values. Help

    Damn, what a database is that: sexual orientation, alcohol consumption, ethnic category! I hope I don't get an ethical dilemma if I help you

    Have a look at QWizard::setDefaultProperty(). There you can change to return type to currentText.

  4. #4
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: field() method of QWizard returning currentIndex of combo-boxes, not values. Help

    Thank you both for your responses!

    As it happens, the setDefaultProperty isn't useful to me in this instance. I was assuming that the combo-box would be similar to a HTML select-box. I'm very new to desktop application development. I figured that each selectable item could have a value assigned to it but a different text label. In this case, the QComboBoxes were being populated by QSqlTableModel objects, so I was hoping that I could display one table column as the text and have the primary key 'id' column, for example, as the "value" of the item. Unfortunately, that doesn't appear to be the case.

    norobro, your answer is the similar to the solution I found! I ended up having to pull the data back from the table model, based on the currentIndex of the QComboBox and the column I wanted to access, using a QModelIndex. Essentially, I'm doing the following:

    Qt Code:
    1. model->setData(model->index(row, Patient::EmploymentStatus),
    2. model->employmentStatus()->index(field("employment_status").toInt(), model->employmentStatus()->fieldIndex("code")).data());
    To copy to clipboard, switch view to plain text mode 

    I don't have direct access to the QComboBox object at the stage I'm trying to access the values, which is why I couldn't do ui->comboBox->blahblah and instead had to pull from the model again.

    I'm pretty sure that what I'm doing is fairly inefficient, so if there's a better way of doing this, I'd love to know. In general, if there are any in-depth examples of working with QSql*Models then I'd love to know about them.

    By the way, Lykurg, the database makes up part of a patient management system for general practice clinics (built for my university project) and those attributes are a small sample of the details that the NHS stores.

    Thanks again, guys.

Similar Threads

  1. Replies: 6
    Last Post: 13th October 2011, 00:14
  2. QWizard and mandatory field problems
    By Judd in forum Qt Programming
    Replies: 2
    Last Post: 5th August 2009, 11:50
  3. Replies: 1
    Last Post: 16th November 2007, 12:06
  4. currentIndex().internalPointer() problem
    By xgoan in forum Qt Programming
    Replies: 2
    Last Post: 14th December 2006, 09:55
  5. Static field and public method
    By probine in forum General Programming
    Replies: 1
    Last Post: 5th March 2006, 11:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.