PDA

View Full Version : Qcombobox dinamically



juliano.gomes
16th July 2015, 00:57
Greetings,

in python i used to set the QComboBox data and get part from my SQL using dictionaries (https://docs.python.org/2/tutorial/datastructures.html#dictionaries), just like the code below:


// create a dict as a object, with this format: 'combobox.currentText':'table.field'
tcDic={'ID':'customer.id','Name':'customer.name', 'Phone':'customer.phone'}

// add these keys in combobox: ID, Name, Phone
combobox.addItems(tcDic.keys())

// in my custom sql, get dinamically the table field in the tcDic related with combobox.currentText
cur.execute("SELECT cutomer.id, customer.name, customer.phone FROM customer WHERE " + tcDic[str(self.combobox.currentText())] + "LIKE % " + str(lineedit.text() + " %")


this way for me was simple and now i need to do something like that in C++. Someone would know if there a similar way or another way to do this kind of implementation?

observation: I know to reference objects straight in SQL is not recomended. I done this only for simplify this example

Best Regards!
Juliano

anda_skoa
16th July 2015, 09:19
You can use a QHash<QString, QString> as the dict



QHash<QString, QQString> tcDict;
tcDict["ID"] = "customer.id";

cur.execute("SELECT cutomer.id, customer.name, customer.phone FROM customer WHERE " + tcDict[comboBox->currentText()] ....


Cheers,
_

juliano.gomes
16th July 2015, 15:44
You can use a QHash<QString, QString> as the dict



QHash<QString, QQString> tcDict;
tcDict["ID"] = "customer.id";

cur.execute("SELECT cutomer.id, customer.name, customer.phone FROM customer WHERE " + tcDict[comboBox->currentText()] ....


Cheers,
_


Perfect!
Thank you very much