How to retrieve the value of a QSqlField using Pyside?

100 Views Asked by At

It should be so simple.

    record = self.acctModel.record(self.ui.cbSelectAccount.currentIndex())
    field=QSqlField()
    field = record.field("AccountName")
    print(field.value)

When I print the field value, I get the text <built-in method value of QSqlField object at 0x02BD9568>

How do I get this value?

The use case is that I have a combobox at the top of the window, and when I select that, I need to change the text in a lineEdit control to match the QSqlTable model that the combobox is bound to.

2

There are 2 best solutions below

0
On

Yay...I Found it!!

This is the link that steered me in the right direction

record = self.acctModel.record(self.ui.cbSelectAccount.currentIndex())
print(record.value("AccountName"))
0
On

I'm adding this answer to complete your question for future followers, because your own answer doesn't really answer your question, you're doing a different thing. Of course it's OK, but you asked "how to retrieve a value of a QSqlField".

In your code, if you included str command and forgotten (), it would work:

print(str(field.value()))

Note, that QSqlField.value() returns QVariant and may in fact contain different data types. Using:

    typeName = QVariant.typeToName(field.type())
    print("Type name:  " + typeName + "    value = " + str(field.value()))

... on some sample data I have here gives:

Type name:  QString    value = Blue Wheels
Type name:  bool       value = 0
Type name:  bool       value = 1
Type name:  QString    value = IL11370852
Type name:  QString    value = WHEEL
Type name:  int        value = 3