Results 1 to 3 of 3

Thread: Model/View with sliders

  1. #1
    Join Date
    Nov 2011
    Qt products
    Unix/X11 Windows

    Default Model/View with sliders

    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?!


  2. #2
    Join Date
    Mar 2011
    Hyderabad, India
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    MacOS X Unix/X11 Windows
    Wiki edits

    Default Re: Model/View with sliders

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    jms (3rd August 2013)

  4. #3
    Join Date
    Nov 2011
    Qt products
    Unix/X11 Windows

    Default Re: Model/View with sliders

    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!

Similar Threads

  1. Replies: 4
    Last Post: 18th April 2012, 18:11
  2. Model/view, apply a filter on model
    By remy_david in forum Qt Programming
    Replies: 4
    Last Post: 4th February 2011, 17:13
  3. Replies: 0
    Last Post: 21st April 2010, 12:23
  4. Replies: 1
    Last Post: 1st February 2010, 18:42
  5. QSql Model-View: How to keep view in sync with model
    By schall_l in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2008, 23:31

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.