Results 1 to 9 of 9

Thread: Using Qt::UserRole in the Model/View architecture

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using Qt::UserRole in the Model/View architecture

    Thanks. I think I basically understand this now. I was hoping I might be able to use user roles as an automatic part of the framework but apparently I can't.

    My implementation problem is this: I have implemented a custom QTableView that contains QwtPlot instances in the cells of two columns. The solution I implemented was to implement a slot for the model's modelReset() signal. In this slot, I create the QwtPlot and use setIndexWidget() to put them into the view. This seems to me to be a kludgy way to do this, but it works.

    I had tried using a custom delegate to wrap the QwtPlot instance, but I couldn't find an appropriate place to create the QwtPlot instance. The createEditor() method isn't it, because the cell isn't editable, and my understanding is that the same delegate instance could be used for the entire view (if calling QAbstractitemView::setItemDelegate()), so I can't create the widget in the delegate constructor.

    So, is there a better (i.e. more elegant) way to accomplish what I'd like to do using user roles and/or delegates that doesn't involve this out-of-band kludge?

    I've attached a screenshot of this table.

    QwtPlotTable.jpg
    Last edited by d_stranz; 21st January 2011 at 23:28.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using Qt::UserRole in the Model/View architecture

    if you use setIndexWidget then the widget you set has no relation to the model-view architecture -- it is just a standalone widget positioned somewhere in the view widget.

    if you want to display a plot in a view then a better solution is to implement a custom delegate that will (in its paint routine) use QwtPlot::print or something similar to render a plot in the item without explicitly showing any widgets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    d_stranz (23rd January 2011)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using Qt::UserRole in the Model/View architecture

    Quote Originally Posted by wysota View Post
    if you use setIndexWidget then the widget you set has no relation to the model-view architecture -- it is just a standalone widget positioned somewhere in the view widget.
    Right. That's why I consider it a kludge.

    Quote Originally Posted by wysota View Post
    if you want to display a plot in a view then a better solution is to implement a custom delegate that will (in its paint routine) use QwtPlot:rint or something similar to render a plot in the item without explicitly showing any widgets.
    That would be very "heavy", since I would have to temporarily instantiate a QwtPlot, configure it (which is where the user role data would come in), render it, then delete it, on every paint event on every delegate that contained a plot. I understand that using a custom delegate would be the correct way to do it within the M-V architecture.

    I'll think about that for the next time I need to do something like this. The current project is too close to release to do so much experimentation.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using Qt::UserRole in the Model/View architecture

    Quote Originally Posted by d_stranz View Post
    That would be very "heavy", since I would have to temporarily instantiate a QwtPlot, configure it (which is where the user role data would come in), render it, then delete it, on every paint event on every delegate that contained a plot. I understand that using a custom delegate would be the correct way to do it within the M-V architecture.
    I don't know Qwt very much unfortunately but maybe there is a class that allows you to build a plot that is not a widget, then it should be somewhat lighter. On the other hand if you use setIndexWidget then you end up with a similar architecture anyway, the difference is whether it is tied to the model-view arch. or not. And the overall performance also depends on which solution you choose.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,324
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using Qt::UserRole in the Model/View architecture

    I don't know Qwt very much unfortunately but maybe there is a class that allows you to build a plot that is not a widget, then it should be somewhat lighter. On the other hand if you use setIndexWidget then you end up with a similar architecture anyway, the difference is whether it is tied to the model-view arch. or not. And the overall performance also depends on which solution you choose.
    As I think about this more, the idea of a QwtPlot widget inside a delegate might be feasible. The delegate -can- instantiate a single QwtPlot on construction and use that for all cells. That makes it much lighter weight than my current solution of a widget per cell.

    My users do need to be able to edit by interaction with certain items contained in the plot, the the delegate can handle that by using it's QwtPlot instance as the editor. For normal painting, I can render the widget contents to an image and simply display that in the cell. I should be able to create that image and attach it to the model as user role data. (I'll ignore the fact that models can be shared among views for now). When the view resizes, I will need to recalculate and cache all of the visible images, but I need to do that now with the widgets anyway.

    Thanks for all this useful discussion. It has allowed me time to think about the problem and use your answers to guide me to a better solution.

Similar Threads

  1. Questions about Model-View Architecture in QT
    By Polnareff in forum Newbie
    Replies: 4
    Last Post: 4th June 2010, 15:01
  2. Replies: 3
    Last Post: 30th March 2010, 13:06
  3. Model/View one index.column as hidden UserRole
    By doitux in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2008, 12:08
  4. Replies: 1
    Last Post: 30th November 2007, 11:55
  5. Replies: 1
    Last Post: 1st March 2006, 11:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.