Results 1 to 6 of 6

Thread: View-model, best strategy

  1. #1
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Question View-model, best strategy

    Hello,
    i need to choose a strategy before going ahead. My problem is quite simple :
    I have a 2d polygon, or node list in a C++ or Python array, I want two views of this polygon (without diplicate node data) :
    - one graphical view, with mouse drag-and-drop abilities (i move nodes with the mouse)
    - one table view with usual editing abilities.

    Of course, i want my two views to be synchronized, so i use view-model programming.
    I've created a custom model for data
    The QTableView "as is" is perfect for table view/edit

    My question is : for graphical view, do i use a custom Delegate or a custom AbstractView ?
    Any advice, (link, template ?, piece of code ??) on this problem is welcome.

    N.B. I've explored QT examples, and chart example seems to drive me to a custom view...
    Thanks for your answer.

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: View-model, best strategy

    Hi,

    I suggest you to create your own Delegate class and implement paint(...) inside it.

    There are plenty of code pieces in the Qt Doc like;

    Qt Code:
    1. class SpinBoxDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SpinBoxDelegate(QObject *parent = 0);
    7.  
    8. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    9. const QModelIndex &index) const;
    10.  
    11. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    12. void setModelData(QWidget *editor, QAbstractItemModel *model,
    13. const QModelIndex &index) const;
    14.  
    15. void updateEditorGeometry(QWidget *editor,
    16. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    17. };
    To copy to clipboard, switch view to plain text mode 

    and you need to add paint method to do painting

    Qt Code:
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: View-model, best strategy

    If you don't want to duplicate data then you need a custom QAbstractItemView. If you can live with data duplication then it might be useful to implement the graphical view using QGraphicsView.

    @zgulser: A delegate is not enough as it doesn't allow to determine where each item is positioned and that's what OP wants.
    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.


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

    puiseux (21st January 2013)

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: View-model, best strategy

    I vote for the way wysota suggested, and here are some thoughts.

    Go with custom QAbstractItemView, if you are dealing with just one Polygon (or may be couple of them). In a case you are planning for more (in order of ~100) then I will opt for QGraphicsView, but note that you need to figure out a good way to keep the QGraphicsSceneand QAbstractModelin sync
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    puiseux (21st January 2013)

  7. #5
    Join Date
    Jun 2009
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: View-model, best strategy

    Ok, I have
    - myscene = QGraphicsScene(),
    - mymodel = QStandardItemModel(),
    - myview=QGraphicsView(),
    - a lot of items in mysceme,

    I am able to move oneitem=QGraphicsItem() with mouse, and now, I want myscene (and mymodel) to be informed about :

    - which item has been moved
    - what is the new position.

    Do i need Qt signal/slot mechanism or is there a (good) way to deal with mouse events ?
    I'm quite beginner with 'event programming', it's a subject full of shadows for me.

    Thank you for your answer.

  8. #6
    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: View-model, best strategy

    QGraphicsItem::itemChange() will let an item know something has happened to it. As for the scene, you should implement events in such a way that you know what is going on there. Then you can inform the model about it.
    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.


Similar Threads

  1. Replies: 4
    Last Post: 18th April 2012, 18:11
  2. Model/view, apply a filter on model
    By remy_david in forum Qt Programming
    Replies: 4
    Last Post: 4th February 2011, 17:13
  3. Replies: 0
    Last Post: 21st April 2010, 12:23
  4. Replies: 1
    Last Post: 1st February 2010, 18:42
  5. QSql Model-View: How to keep view in sync with model
    By schall_l in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2008, 23:31

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.