Results 1 to 5 of 5

Thread: Model - View Programming doubt.

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Model - View Programming doubt.

    Hi,

    I have an application in which data is stored in 10 tables (SQLITE) and the data is to be extracted from almost all of them and then shown in a custom view. Here are a few doubts :

    1. Does having 10 tables and using all of them for view means that I will need to have 10 QAbstractTableModel (or QSqlQueryModel or QSqlTableModel.. I dont knw which one to use)?

    2. Since my view is custom, how can I set the model to it ?

    3. Since my view is custom, how can I update each model when user changes something in the view ? (Add row or change an item or something similar).

    4. Is there a way by which I can use MVC design without using Qt's MVC framework ?

    Please Help.
    Thanks a lot.

  2. #2
    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: Model - View Programming doubt.

    Quote Originally Posted by munna
    1. Does having 10 tables and using all of them for view means that I will need to have 10 QAbstractTableModel (or QSqlQueryModel or QSqlTableModel.. I dont knw which one to use)?
    No. It depends on the model you use. For QSqlQueryModel you can have a single model for all of the tables as you can query more than one table at the same time. For QSqlTableModel you'll need a separate model for each table. With a custom model, it depends only from you, as you implement the model.


    2. Since my view is custom, how can I set the model to it ?
    3. Since my view is custom, how can I update each model when user changes something in the view ? (Add row or change an item or something similar).
    It depends what do you mean by "custom". If it inherits QAbstractItemView, then you can use setModel(). If it doesn't, then it's not really a view and you have to implement everything by yourself.

    4. Is there a way by which I can use MVC design without using Qt's MVC framework ?
    Sure, you can implement one yourself. Use signals and slots for that.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model - View Programming doubt.

    Quote Originally Posted by munna
    Does having 10 tables and using all of them for view means that I will need to have 10 QAbstractTableModel (or QSqlQueryModel or QSqlTableModel.. I dont knw which one to use)?
    If you have one view, you need one QSqlQueryModel, QSqlRelationalTableModel or a custom model.

    Quote Originally Posted by munna
    Since my view is custom, how can I set the model to it ?
    It's still QAbstractItemView --- use setModel() method.

    Quote Originally Posted by munna
    Since my view is custom, how can I update each model when user changes something in the view ? (Add row or change an item or something similar).
    If your view was implemented properly, it should happen automagically (provided that model is editable).

    Quote Originally Posted by munna
    Is there a way by which I can use MVC design without using Qt's MVC framework ?
    Yes --- implement everything yourself.

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model - View Programming doubt.

    Thanks for prompt responses.

    Quote Originally Posted by wysota
    It depends what do you mean by "custom". If it inherits QAbstractItemView, then you can use setModel(). If it doesn't, then it's not really a view and you have to implement everything by yourself.
    I am not sure if can subclass QAbstractItemView and that is why I say custom view.
    My view is something similar to that of mac's addressbook application. The keyboard and mouse event's are also more or less similar.

    Currently I do the following :

    1. In my "view" class I have a reference of a class which is responsible for querying the tables and extracting the data from it. Therfore member functions of view class call member functions of the "model" (I doubt if I call it a model) class and then set the data in the view.

    2. When the user is done with editing I send back the information to the "model" which updates the table.

    Can you please tell me if what I do is a good design or not ? Also, can you suggest a better way to achieve it. I would be really greatful to you guys if you can suggest me some link or book which would be of some help.

    Thanks a lot.

  5. #5
    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: Model - View Programming doubt.

    Quote Originally Posted by munna
    I am not sure if can subclass QAbstractItemView and that is why I say custom view.
    QAbstractItemView provides methods to communicate with the model and exchange information.

    My view is something similar to that of mac's addressbook application. The keyboard and mouse event's are also more or less similar.
    It doesn't matter. It matters whether you use an existing view or its subclass or implement everything from scratch.

    1. In my "view" class I have a reference of a class which is responsible for querying the tables and extracting the data from it. Therfore member functions of view class call member functions of the "model" (I doubt if I call it a model) class and then set the data in the view.

    2. When the user is done with editing I send back the information to the "model" which updates the table.

    Can you please tell me if what I do is a good design or not ?
    If it works for you, then it's fine.

    Also, can you suggest a better way to achieve it. I would be really greatful to you guys if you can suggest me some link or book which would be of some help.
    Hard to say without knowing the structure and behaviour of your data. I would probably stick to Qt's model-view approach.

    About a custom view, you can look at the Pie chart example provided with Qt or at my Chart view. They both provide something which we call a custom view (a subclass of QAbstractItemView).

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. A few queries about Model View Programming
    By montylee in forum Qt Programming
    Replies: 46
    Last Post: 2nd March 2009, 08:36
  3. model View programming problem
    By mismael85 in forum Qt Programming
    Replies: 3
    Last Post: 2nd April 2008, 21:44
  4. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

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.