23

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.

San Jacinto
  • 8,774
  • 5
  • 43
  • 58
  • 13
    +1 For some reason, the idea that it should be simple to bind UI elements to a database or a model in memory never made it far outside the Windows world. While it's possible to with Qt signals, it's still a lot of boiler plate code and very tedious :( – Aaron Digulla Nov 09 '09 at 15:42
  • 1
    It's actually a very old idea that predates .Net. I was using it in the OOFILE forms frameworks back in 1997 and I've seen code generated by AppMaker of an even earlier vintage that generates bindings even targeting pure C code. – Andy Dent Aug 04 '10 at 05:21

3 Answers3

8

As the user nonchalant mentioned in a comment you can use the QDataWidgetMapper class. This is quite an easy way of binding arbitrary widgets to data that is stored in a QAbstractItemModel.

The example on the linked page shows in a few lines of code, how you can link your data model to common used input widgets:

QDataWidgetMapper *mapper = new QDataWidgetMapper;
mapper->setModel(model);
mapper->addMapping(mySpinBox, 0);
mapper->addMapping(myLineEdit, 1);
mapper->addMapping(myCountryChooser, 2);
mapper->toFirst();
tomvodi
  • 5,577
  • 2
  • 27
  • 38
7

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...

cybevnm
  • 2,586
  • 4
  • 30
  • 33
3

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.

Sohail
  • 3,020
  • 1
  • 25
  • 23
  • 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 Nov 09 '09 at 16:47
  • 3
    I strongly suggest you look at QDataWidgetMapper which works for widgets not under the MVC framework. – Sohail Nov 10 '09 at 03:46