Results 1 to 4 of 4

Thread: QwtPlot MVC

  1. #1
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QwtPlot MVC

    Hello,

    I have a QwtPlotCurve attached to QwtPlot. The curve is made up of a large number of QPointF's. I want to keep only one copy of these points in memory(so I need to avoid deep copies). Hence I chose QwtPointSeriesData(Vector<QPointF>&). I use QwtPlotCurve::setSamples() to make sure my curve reflects any changes that happen to the vector of points. I provide appropriate boundingRect() and everything works fine.

    Code snippet:
    Qt Code:
    1. m_mainPlot = new QwtPlot(this);
    2. curve1 = new QwtPlotCurve("Curve 1");
    3. QVector<QPointF> m_2dPointsVector;
    4. curve1->setSamples(QwtPointSeriesData(m_2dPointsVector));
    To copy to clipboard, switch view to plain text mode 

    I want to use Qt's Model-View pattern and create a Graph which acts like my View. The graph which is a QWidget has a child QwtPlot. Create a QAbstractListModel to hold the data: QVector<QPointF> m_2dPointsVector;. Everytime this data changes the QwtPlotCurve updates itself as I described before. But to reflect this change in my Graph I need to listen to the dataChanged() signal from the QAbstractListModel. Inside the slot I call the QwtPlot::replot(). This works fine!

    My Question is:
    I think I am not doing it right since the graph is a normal widget which has a pointer to the QAbstractListModel and listen's to its dataChanged() signal. Is there a cleaner way of doing this so that I can make the best use of Qt's Model-View pattern?

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot MVC

    Quote Originally Posted by Vikram.Saralaya View Post
    Is there a cleaner way of doing this so that I can make the best use of Qt's Model-View pattern?
    The model API is just too slow/fat for iterating over many points and the concept of working around type checks of the compiler by hiding the data behind a generic API does not make much sense, when every class knows that it is a QVector<QPointF>.
    So IMHO the best way to use Qt's Model-View pattern for this particular use case is - not to use it. If you want to have a MVC design for your plot better implement your own without using QAbstractItemModel.

    Just my 2 cents,
    Uwe

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

    Vikram.Saralaya (28th May 2015)

  4. #3
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QwtPlot MVC

    Hello Uwe,

    That's indeed very helpful. After playing with it for a while I did strip off all the model view constructs of Qt.

    I treat QwtPointSeriesData as my model now. QwtPlotCurve is my view. QVector<QPointF> is my data. All the changes to my data is obtained by the curve since it references to it. But to update the curve I need to call QwtPlot::replot(). Do you think I need to emit a signal when data changes and call replot() manually? or is there a way the model-view structure used internally in Qwt can provide a refresh/replot ?!

    Regards
    Vikram
    Last edited by Vikram.Saralaya; 28th May 2015 at 07:29.

  5. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot MVC

    The auto-replot feature ( QwtPlot::autoReplot() ) is triggered whenever QwtPlotItem::itemChanged() is called. This happens f.e when assigning a new data set.
    When you plan to change the data behind the back of the QwtPointSeriesData you would have to add some sort of notification, so that itemChanged() gets called.

    But I would be careful with any auto-update patterns - look always nice in books, but in reality you soon lose control over the program flow and when expensive operations are involved ( like replot of heavy plots ) it might end in low performance.

    Uwe

  6. The following user says thank you to Uwe for this useful post:

    Vikram.Saralaya (28th May 2015)

Similar Threads

  1. Replies: 9
    Last Post: 25th October 2012, 20:55
  2. QwtPlot + UI problem
    By petromp in forum Qwt
    Replies: 3
    Last Post: 5th April 2012, 22:35
  3. findChildren<QwtPlot *>()
    By gib in forum Qwt
    Replies: 0
    Last Post: 23rd March 2010, 04:09
  4. double QwtPlot
    By mastupristi in forum Qwt
    Replies: 0
    Last Post: 9th July 2009, 14:25
  5. Replies: 6
    Last Post: 14th May 2009, 13:02

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.