Results 1 to 5 of 5

Thread: Questions about Model-View Architecture in QT

  1. #1
    Join Date
    May 2010
    Posts
    41
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Questions about Model-View Architecture in QT

    Hi,

    So I've been reading a lot about the Model-View-Delegate architecture that QT employs. I've noticed that this is used primarily with widgets like the ListView, TreeView, and TableView and ColumnView. In my own code, I'd like to be able to have a Model for other widgets, like QTexEdit or QSpinBox. It seems, though, that this is only possible if I create a widget which inherits from something like QAbstractItemView. While there is tons of documentation on creating your own models and delegates, there doesn't seem to be much for views, and it seems like a lot more work.

    I read about Delegates here, and as far as I can make out, it'll allow for attaching a data model to a widget as I described, but the delegate itself must still be a delegate of a view, of course, and so the problem persists.

    What exactly is the correct thing to do here, and is there perhaps any documentation? Thanks

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Questions about Model-View Architecture in QT

    Quote Originally Posted by Polnareff View Post
    In my own code, I'd like to be able to have a Model for other widgets, like QTexEdit or QSpinBox.
    Can you elaborate a bit on what you want to do... give us an example of the kind of problem you’re trying to solve?

    The whole idea of what Qt calls a “model” is to manage a collection of data — a list, a table, a tree or some hybrid of those — in a consistent way that separates maintaining the data from displaying the data. Since QTextEdits and QSpinBoxes don’t display collections... it’s unclear, at least to me, what you’re hoping to accomplish.

    If you really do want to display a Qt model — something derived from QAbstractItemModel — in a custom GUI widget, then subclassing QAbstractItemView would usually be the way to go.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about Model-View Architecture in QT

    Have a look at QDataWidgetMapper
    It seems, though, that this is only possible if I create a widget which inherits from something like QAbstractItemView. While there is tons of documentation on creating your own models and delegates, there doesn't seem to be much for views, and it seems like a lot more work.
    If only you had gone through the Qt Demos properly !!
    Theres a simple data widget mapper in it.

  4. #4
    Join Date
    May 2010
    Posts
    41
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about Model-View Architecture in QT

    Quote Originally Posted by Coises View Post
    Can you elaborate a bit on what you want to do... give us an example of the kind of problem you’re trying to solve?

    The whole idea of what Qt calls a “model” is to manage a collection of data — a list, a table, a tree or some hybrid of those — in a consistent way that separates maintaining the data from displaying the data. Since QTextEdits and QSpinBoxes don’t display collections... it’s unclear, at least to me, what you’re hoping to accomplish.
    I'd like to, as you say, separate maintaining the data and displaying it. I'm working on software for a company, and we'd like to employ the MVC architecture to our code, or at least some form of it, so as to ensure scalability and, well, just plain organization

    Quote Originally Posted by aamer4yu View Post
    Have a look at QDataWidgetMapper
    If only you had gone through the Qt Demos properly !!
    Theres a simple data widget mapper in it.
    Haha, my mistake! I spent a lot of time looking through the demos my first week with Qt, but after a while, hadn't thought to return to them. I'll check it out. Thanks.

  5. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Questions about Model-View Architecture in QT

    Quote Originally Posted by Polnareff View Post
    I'd like to, as you say, separate maintaining the data and displaying it. I'm working on software for a company, and we'd like to employ the MVC architecture to our code, or at least some form of it, so as to ensure scalability and, well, just plain organization
    Qt’s “model/view” framework can help when the model takes the form of a table, a tree or some hybrid of the two — the kind of thing where the view would be something like a QListView, a QTableView or a QTreeView. You certainly can create your own custom views, but if one of the standards isn’t at least close to what you want, there’s a good chance Qt’s model/view isn’t what you want.

    That’s not to say you can’t program other forms of model/view or model/view/controller architecture in Qt — you just don’t use what Qt calls “model/view,” because it won’t help. You are largely on your own to divide your program logic in whatever way works best for you; but two Qt features that are particularly likely to be useful in general model/view/controller programming are Signals and Slots and Properties.

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

    Polnareff (4th June 2010)

Similar Threads

  1. Replies: 3
    Last Post: 30th March 2010, 13:06
  2. Questions on Model/View
    By joshuajcarson in forum Qt Programming
    Replies: 7
    Last Post: 26th September 2008, 21:29
  3. Replies: 1
    Last Post: 30th November 2007, 11:55
  4. Questions regarding setting up a Qt SQL Model/View
    By Methedrine in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2007, 09:26
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.