Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Multiple widget instances



  • Hello,
    Is there a general way to have 1 model for a widget and 2 or more views, for example multiple instances of a QComboBox and all of whose values (except perhaps position,etc) of them are synchronised across the application, without having to define custom methods/delegates for each Qwidget?

    Thanks,
    rpqt



  • Have not tried it, but I would assume setting the model of one QComboBox to the model used by another QComboBox would do exactly that. E.g.:
    @
    comboBox1->setModel(comboBox2->model());
    @
    Or, if you have created the model separately:
    @
    comboBox1->setModel(comboBoxModel);
    comboBox2->setModel(comboBoxModel);
    @
    The model can be any subclass of QAbstractItemModel.



  • [quote author="rpqt" date="1331714339"]Hello,
    Is there a general way to have 1 model for a widget and 2 or more views, for example multiple instances of a QComboBox and all of whose values (except perhaps position,etc) of them are synchronised across the application, without having to define custom methods/delegates for each Qwidget?[/quote]

    Are you talking about the model/view concept of Qt?
    Or is it a different direction, you are looking at?



  • Thanks @ludde and @gerolf, but what I meant was that suppose a user wanted to create a copy of a (maybe rather complex subclass of) QWidget or QGLWidget by dragging it (or a key combination) such that the new widget would (except for being located in a different place) share all the data member values etc of the first.
    Now for a combobox, as ludde mentioned, there is a set model method (under MVC) but is there a more generic way like SetWidgetModel or something (even if it is not exactly MVC) that can replicate state across 2 widgets of the same class without having to explicitly define custom methods/signals/delegates for each user interaction of such widget class or store the changed values in some data structure (for each type)?

    Thanks,
    rpqt



  • OK, that's something much more generic, which I think will be quite difficult to accomplish, at least in a generic way. I'm pretty sure there is no built-in support for anything like that in Qt.

    For a specific QWidget subclass, I guess you can make sure that data is shared accross instances of the class, or synchronize changes by setting up signals and slots to propagate data/property changes. But this will be something you will have to do separately for/in each such QWidget subclass, since it is not really possible to know which data to synchronize and how to do it for the generic case.


Log in to reply