PDA

View Full Version : View-model, best strategy



puiseux
21st January 2013, 12:52
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.

zgulser
21st January 2013, 13:18
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;




class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT

public:
SpinBoxDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;

void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};


and you need to add paint method to do painting



QAbstractItemDelegate::paint()

wysota
21st January 2013, 13:19
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.

Santosh Reddy
21st January 2013, 14:31
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 QGraphicsScene and QAbstractModel in sync

puiseux
29th January 2013, 15:30
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.

wysota
29th January 2013, 15:33
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.