Qt equivalent of .NET data binding?

前端 未结 3 1974
北恋
北恋 2021-02-07 02:07

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.

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-07 02:34

    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();
    

提交回复
热议问题