PDA

View Full Version : reset value of combobox based on stored data in db



rdjenner
27th August 2010, 15:16
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.

waynew
27th August 2010, 23:27
I don't know about mapper, but here is how I initialize my combo boxes with db values:



// Retrieves the preferences to populate the dialog when it opens
void PreferencesDialog::getPreferences(QString ctrlConn)
{
QSqlDatabase db = QSqlDatabase::database(ctrlConn);
db.open();
QSqlRecord record;
QSqlTableModel* model = new QSqlTableModel(this, db);
model->setTable("preferences");
model->select();
record = model->record(0);

m_ui->cbPort->setCurrentIndex(m_ui->cbPort->findText(record.value("port").toString()));


Does that help any?

Urthas
28th August 2010, 00:19
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.

rdjenner
28th August 2010, 13:04
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

waynew
28th August 2010, 13:17
How are you recalling the db data? What does the code look like?