PDA

View Full Version : Model/View with sliders



jms
3rd August 2013, 01:12
I am trying to figure out how to use Qt's model/view architecture to do the following -

Have a QSlider, two QPushButtons (to increment or decrement the slider's value), and a QSpinBox for changing the increment/decrement value.

In a model, store:
- min/max value of the slider (the range)
- the current value on the slider
- the increment value

I also need three of these (one each for x, y, and z); they will control the movement of an instrument on its three axes.

I would like to use Model/View since the instrument needs to be able to set the min/max in the model and the user needs to be able to control movement (via the QSliders or QPushButtons). The instrument can move by itself (and update the model), and I need this to be reflected in the View (set the QSliders at their appropriate positions if the instrument moves without the user changing a QSlider). It would be nice if the min/max could change (for example if units are switched from say microns to millimeters, etc.).

I am pretty sure I could get all of this working by subclassing QTableView, using delegates for the QSliders, QPushButtons, and QSpinBoxes, and subclassing QAbstractTableModel. Then I'd have something that would look like:

X: [-]<-------|------>[+][1 um]
Y: [-]<--|----------->[+][5 um]
Z: [-]<-----------|-->[+][4 um]

If I understand correctly, I'd have to call QTableView::setPersistentEditor() on each of the QModelIndexes in the model (corresponding to each of the widget delegates) to get the 'editor' widgets to stay shown in the view all the time.

I am asking here because I don't want to stuff all the widgets in the same table, ideally I would be able to treat the QSlider, 2x QPushButton, and QSpinBox for each axis as one widget and put it wherever I want in the UI, with arbitrary orientation (probably horizontal for X, vertical for Y and Z).

It is not clear to me how to accomplish this with Qt's model/view classes. Does anyone have an guidance? Not looking for code, just a general approach or your insights.

Should I use one model with three views (x, y, z)?
Should I have three models, three views?
Is there a way to do this with one model and one view?
Am I even making sense?!

TIA

Santosh Reddy
3rd August 2013, 06:20
You could use Qt Model/View classes or write custom classes using similar concept as MVC

Rather that using MVC classes I would prefer writing a composite widget and a custom model (not derived from Qt MVC classses).

In any case you will need to write a custom composite widget, and IMO using Qt MVC here will only complicate it.

jms
3rd August 2013, 07:33
If there is no obvious way to do this with Qt's classes for model/view I'll just write my own.
I was worried that I was missing something obvious.

Thank you! :)