tags:

views:

677

answers:

2

Is there an equivalent of .NET's data binding in Qt?

I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner if I could bind the data to these strings rather than either querying the database again based off of a new combobox selection or some other scheme based off of building my own index of entities that would be searched with the QStrings.

The best I've come up with is to derive these entities from QString and pushing them into the widgets this way, but I've yet to actually try it. I'm not sure if it will work the way I want it to, and it seems like a nasty hack.

If there is no data binding, what do you suggest?

Thank you.

+2  A: 

Well, for combobox specifically, you can set a model. For QObjects in general you can use the notify signal for properties to connect or other non-property related signals. I think there is another way to do it but I can't recall.

cheez
This seems like the way to go; thanks. I've new to Qt, and your suggestion about the Models seems the easiest way to do this. I think this is a pretty ugly approach to data binding, but it seems to yield the functionality that I want. Thanks again.
San Jacinto
I strongly suggest you look at QDataWidgetMapper which works for widgets not under the MVC framework.
cheez
+2  A: 

One way is using Qt Model/View Classes (with base at QAbstractItemModel), but they need that you widget inherits QAbstractItemView (this is widgets like QTableView etc.).
If you want map Qt model to set of widgets, which haven't nothing common with QAbstractItemView you can use QDataWidgetMapper, which maps separate widget to Qt Model/View indexes. But anyway, as said Aaron Digulla, you must write some boiler plate code...

vnm