QDataWidgetMapper将多个列映射到单个自定义窗口小部件的多个属性

时间:2018-11-08 16:41:22

标签: c++ qt model-view-controller qwidget

为此,我一直在寻找解决方案。情况如下:

我有一个自定义滑块类,该类具有两个控制两个double范围的句柄,并为此创建了四个属性:

Q_PROPERTY(double minimum MEMBER mMinimum NOTIFY minimumChanged USER true)
Q_PROPERTY(double maximum MEMBER mMaximum NOTIFY maximumChanged USER true)
Q_PROPERTY(double lowerValue MEMBER mLowerValue NOTIFY lowerValueChanged USER true)
Q_PROPERTY(double upperValue MEMBER mUpperValue NOTIFY upperValueChanged USER true)

我将这些属性绑定到QDataWidgetMapper:

settingsMapper->addMapping(ui->cutLevelSlider, settings.indexOf("lowerCutLevel"), "lowerValue");
settingsMapper->addMapping(ui->cutLevelSlider, settings.indexOf("upperCutLevel"), "upperValue");
settingsMapper->addMapping(ui->cutLevelSlider, settings.indexOf("minValue"), "minimumValue");
settingsMapper->addMapping(ui->cutLevelSlider, settings.indexOf("maxValue"), "maximumValue")
settingsMapper->toFirst();

,希望我可以在滑块和settings项目模型之间进行两种方式的更新,该模型可以管理设置值的单个事实。换句话说,我正在尝试使用滑块cutLevelSlider来查看存储在模型settings中的4个值min,max,lower,upper。

但是它不起作用。当我滑动滑块时,只有滑块的最后一个属性(upperValue)会更新到模型,其他所有属性都不会改变。当我通过另一个小部件(例如,从表格视图进行编辑)更改模型时,滑块状态不会相应更改。

有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

选中https://forum.qt.io/topic/112644/qdatawidgetmapper-to-map-multiple-columns-to-multiple-properties-of-single-custom-widget/3

self.mapper = QDataWidgetMapper(self)
        self.mapper.setModel(model)
        self.mapper.addMapping(self.btnLoad, 7, b'enabled')

        self.mapper2 = QDataWidgetMapper(self)
        self.mapper2.setModel(model)
        self.mapper2.addMapping(self.btnLoad, 8, b'text')

        self.mapper.toFirst()
        self.mapper2.toFirst()