[PyQt] QComboBox: display text but pass on Unique Id (MySql)

Cristobal Infante cgcris at gmail.com
Fri Nov 18 22:50:06 GMT 2011


ok got the answer:

        uniqueIdColumn = 0
        uniqueId =
model.data(model.createIndex(Index,uniqueIdColumn,0),QtCore.Qt.DisplayRole)

I am working with python 2.7 so had to set the API v2...

import sip
sip.setapi('QVariant', 2)



On 16 November 2011 22:44, Cristobal Infante <cgcris at gmail.com> wrote:

> I havent found the soultion for this yet, it's driving me a bit mad
>
> can somebody please answer me this "simple" question:
>
> How do you retrieve a single value from a model if you know the index?
>
> Thanks, Cris
>
>
> On 8 November 2011 22:38, David Boddie <david at boddie.org.uk> wrote:
>
>> On Fri, 4 Nov 2011 13:51:45 +0000, Cristobal Infante wrote:
>>
>> > My first post in this list, a big hello to everyone :D
>>
>> Welcome!
>>
>> > I am just starting with Pyqt but have enough experience with python.
>> >
>> > I am connecting a Mysql Databse nicely, being able to retrieve the
>> complete
>> > table and
>> > get it as a model. I've then been able to display in a combobox a "Name"
>> > column
>> > just by using 'setcolumn(1).
>>
>> OK. So far so good.
>>
>> > Until here is all good, but is passing the Id from that record that's
>> > proven hard to crack for me.
>> > I've been looking around in the net, but havent been able to find an
>> > example using a database.
>> > So display "Jane" put pass on the unique ID of that record.
>>
>> Just an idea: is the record() method of the model what you want? The model
>> should inherit this from QSqlQueryModel.record().
>>
>> > I have an 'activated' function that is being executed everytime the
>> combo
>> > changes, but at the moment it's only retrieving
>> > info from that column that's displayed in the combobox.
>>
>> You can get the corresponding row of the item displayed in the combo box
>> by calling the QComboBox.currentIndex() method. This is effectively the
>> same as the row in the model, anyway. Then pass it to the model's record()
>> method and see what you get.
>>
>> Does that work?
>>
>> David
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20111118/2ff3a57b/attachment.html>


More information about the PyQt mailing list