Results 1 to 9 of 9

Thread: How to identify data from the model in the view?

  1. #1
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to identify data from the model in the view?

    Hello,
    I am using Qt's Model/View Programming framework. I have subclassed QAbstractItemView and QAbstractItemModel. In my situation I need to pick out certain specific parts of the model to display in a custom view. I have used a Tree structure in the model class to represent the data. In each item of the tree, I have a column that has a text string with a unique name of my data. The model only changes internally by reading data from a serial port. In the dataChanged slot in the view I check to see which index changed by looking at the string and then update the appropriate widget in the view. I am wondering if this is the best way of doing this. It seems a little hokey to me but I don't really know how else to do it. Thanks for any help!

    csmithmaui
    Last edited by csmithmaui; 16th April 2010 at 11:10. Reason: updated contents

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to identify data from the model in the view?

    I think you overcomplicate things. For what you need custom view? Is there any special way in presenting your data other then tree, list or table? Normally your model should be connected somehow with this serial port data, so you update date stored in model with those new date. Then model should emit QAbstractItemModel::dataChanged() signal with affected index range as an parameter and then view will update those indices using QAbstractItemModel::data() method.
    So my hint is that you do not need any special view (eventually you may need custom delegate if you want custom drawing or custom editing) but you need a model which is made properly.

  3. #3
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to identify data from the model in the view?

    Hi faldzip,
    Thanks for your answer. I am presenting my data other than in a tree, list or table. I am using QLabels to display some of it. I will be using Qwt to display other parts of it. The thing is, I can't just display all of the data in a standard view. I need to be able to pull out certain pieces of the data to display different ways all in the same view. The data is voltages, temperatures, date/time, current measurements. I have looked around for information on how to do this but everything I read of Qt Model/View seems to be for data that is not like mine. Maybe Model/View is not good in this case? Anyway, I know I can make it work, I was just trying to use MVC to have a more elegant solution than I usually do when I write a GUI. Any thoughts/comments you have are much appreciated.

    csmithmaui
    Last edited by csmithmaui; 16th April 2010 at 12:16. Reason: updated contents

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

    Default Re: How to identify data from the model in the view?

    Quote Originally Posted by csmithmaui View Post
    Thanks for your answer. I am presenting my data other than in a tree, list or table. I am using QLabels to display some of it. I will be using Qwt to display other parts of it. The thing is, I can't just display all of the data in a standard view. I need to be able to pull out certain pieces of the data to display different ways all in the same view.
    Have you seen QDataWidgetMapper?

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to identify data from the model in the view?

    So don't use Model/View, because in View you uses labels and other things which are not commonly used there, because it is not the place for QLabels. And don't use Model if you do not use its API in anyway.
    My idea would be to make some class which will read all this things through the serial port with some API for exposing this data to the world in your specific way and have some signals to notify about changes, like:
    Qt Code:
    1. class DataObject : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. // ... c'tor and stuff
    6. double temperature() const;
    7. signals:
    8. void temperatureChanged(double temp);
    9. };
    To copy to clipboard, switch view to plain text mode 

    Add then create widget with all those labels, Qwt things and anything you want and make there slots for reacting for this data changes.

  6. #6
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to identify data from the model in the view?

    @Wysota: No I had never heard of QDataWidgetMapper...interesting. I will check it out.

    @Faldzip: Thanks a lot. This helps clarify things to me. It sounds likea good idea.

    csmithmaui

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

    Default Re: How to identify data from the model in the view?

    Quote Originally Posted by csmithmaui View Post
    @Wysota: No I had never heard of QDataWidgetMapper...interesting. I will check it out.
    Please do it. It's exactly what you need instead of the custom view.

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

    csmithmaui (17th April 2010)

  9. #8
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to identify data from the model in the view?

    @wysota:
    I have been reading up on QDataWidgetMapper and it seems like it will work in my situation. I have a question though.
    In the documentation it says:
    Every time the current index changes, each widget is updated with data from the model via the property specified when its mapping was made.
    What changes the current index? I am assuming that everytime I call setData in the model, the currentindex changes. Just wanted to verify.

    Thanks for your help.

    csmithmaui

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

    Default Re: How to identify data from the model in the view?

    Quote Originally Posted by csmithmaui View Post
    What changes the current index?
    You

    QDataWidgetMapper::currentIndex

Similar Threads

  1. Model/View Programming: a question about data
    By QAlex in forum Qt Programming
    Replies: 1
    Last Post: 10th December 2009, 18:43
  2. Table view->model->set data
    By tulsi in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 08:36
  3. Model-View: how to get the data from my selected rows
    By viasant in forum Qt Programming
    Replies: 5
    Last Post: 11th August 2008, 02:16
  4. data, model and tree view
    By larry104 in forum Qt Programming
    Replies: 17
    Last Post: 3rd July 2006, 14:43
  5. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 20:59

Tags for this Thread

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.