Results 1 to 5 of 5

Thread: reset value of combobox based on stored data in db

  1. #1
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default reset value of combobox based on stored data in db

    I have the following combo box code in my constructor

    {
    ui->setupUi(this);
    ippartNumber = "";

    ui->bushingStructuralcomboBox->clear();
    ui->bushingStructuralcomboBox->addItem("Bushing");
    ui->bushingStructuralcomboBox->addItem("Structural");
    }

    I map the data on the filed using the datamapper as follows

    mapper->addMapping(ui->bushingStructuralcomboBox,model->fieldIndex("Bushing_Structural"));

    The value in the db is either a B or S. When I change the combobox it stores the correct value in the db. My problem is when I pull the record back up, the value in the combo box is always BUSHING. How do I get the combox to recognize the db value and change to STRUCTURAL if the db vlue is S. Please provide example code. I've looked at the docs and examples and can't make sense of it.

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reset value of combobox based on stored data in db

    I don't know about mapper, but here is how I initialize my combo boxes with db values:

    Qt Code:
    1. // Retrieves the preferences to populate the dialog when it opens
    2. void PreferencesDialog::getPreferences(QString ctrlConn)
    3. {
    4. QSqlDatabase db = QSqlDatabase::database(ctrlConn);
    5. db.open();
    6. QSqlRecord record;
    7. QSqlTableModel* model = new QSqlTableModel(this, db);
    8. model->setTable("preferences");
    9. model->select();
    10. record = model->record(0);
    11.  
    12. m_ui->cbPort->setCurrentIndex(m_ui->cbPort->findText(record.value("port").toString()));
    To copy to clipboard, switch view to plain text mode 

    Does that help any?

  3. #3
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: reset value of combobox based on stored data in db

    For the sake of completeness, it is worth mentioning QSettings as well. That approach may or may not be appropriate depending on the scope and intended usage of your application. That is to say: are your users on their own machines or on public workstations? If the former, a database *may* be overkill for remembering some settings, and you can offload work from your server via QSettings.

  4. #4
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reset value of combobox based on stored data in db

    I'm still confused. The drop down options are static. On an existing record, the value in my db record will have either B or S. So on entry of a new record, BUSHING or STRUCTURAL are the only options. When the user makes a selection from the drop down, the correct B or S is stored. What I need to happen is when the record is recalled, for the dropdown to display the proper value, either BUSHING or STRUCTURAL based on the B or S value from the db record in the drop down

  5. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reset value of combobox based on stored data in db

    How are you recalling the db data? What does the code look like?

Similar Threads

  1. member function to read data from a combobox
    By babygal in forum Qt Programming
    Replies: 2
    Last Post: 17th August 2010, 11:44
  2. Replies: 0
    Last Post: 31st July 2010, 17:27
  3. Replies: 1
    Last Post: 9th February 2010, 12:11
  4. QObjects as data members stored by value
    By Radagast in forum Qt Programming
    Replies: 3
    Last Post: 6th August 2009, 19:14
  5. reset QwtPlot
    By sarefo in forum Qwt
    Replies: 1
    Last Post: 24th January 2008, 08:00

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.