Well, I must say, the updating mechanism of yours is really wicked..
All you really need is:
// member variables:
// QPoint offset;
// QPersistentModelIndex index;
{
index = indexAt(e->pos());
offset = index.data().toRect().topLeft() - e->pos();
}
{
if (index.isValid())
{
QRect r
= index.
data().
toRect();
r.moveTopLeft(e->pos());
r.translate(offset);
model()->setData(index, r);
}
}
{
}
{
setDirtyRegion(viewport()->rect());
for (int row = 0; row < model()->rowCount(); ++row)
{
for (int col = 0; col < model()->columnCount(); ++col)
{
}
}
}
// member variables:
// QPoint offset;
// QPersistentModelIndex index;
void RectView::mousePressEvent(QMouseEvent* e)
{
index = indexAt(e->pos());
offset = index.data().toRect().topLeft() - e->pos();
}
void RectView::mouseMoveEvent(QMouseEvent* e)
{
if (index.isValid())
{
QRect r = index.data().toRect();
r.moveTopLeft(e->pos());
r.translate(offset);
model()->setData(index, r);
}
}
void RectView::mouseReleaseEvent(QMouseEvent* e)
{
index = QPersistentModelIndex();
}
void RectView::paintEvent(QPaintEvent* e)
{
QAbstractItemView::paintEvent(e);
QPainter painter(viewport());
setDirtyRegion(viewport()->rect());
for (int row = 0; row < model()->rowCount(); ++row)
{
for (int col = 0; col < model()->columnCount(); ++col)
{
QModelIndex index = model()->index(row, col, QModelIndex());
itemDelegate()->paint(&painter, QStyleOptionViewItem(), index);
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks