You don't need any layer. You can paint in paintEvent directly on the widget. dragEnterEvent will probably help you (should help you mark where to draw the line).
You don't need any layer. You can paint in paintEvent directly on the widget. dragEnterEvent will probably help you (should help you mark where to draw the line).
I've managed to paint on top of the tree now by, like you said, call the paintEvent(). But the thing is that I want the line to follow the mouse and show the drop target.
The problem is that I don't understand how dragEnterEvent can help me... it seems to only start when I start a drag and I need to know how the mouse is changing during the dragging process.
I've tried dragMoveEvent(). The problem there is that I need to repaint the widget while the object is dragging and it only happens when the dragging has stopped. I tried to trigger the paintEvent by calling repaint() from inside the dragMouseEvent(). I wonder if it is the floating icon that is preventing me from repainting the widget... but I can't find out how to turn it off.
thanks for the tip!
/pir
use mouseMove to update cursor position. dragEnter will just help you to know that you are dragging an object.
The problem is that I can't get the treeView to redraw the widget so the line is animated. What happens is that I start dragging and after I have stopped dragging by released the mouse the line is drawn. There is something that prevents the paintEvent to redraw the treeView while I'm dragging...
Did you call update() from mouseMoveEvent?
pir (20th May 2006)
No, I don't think I've tried that. I called repaint from dragMoveEvent().
I should try mouseMoveEvent()...
Ok, I can't make it work.
This is what I have so far... it is mostly taken from the docs, with some deactivated. I hope that isn't the problem...
The problem is that when I start a drag operation the mouseMoveEvent() isn't called during the draging. To be able to react to the dragging I need to implement the dragMoveEvent(). The problem is that if I do this, it doesn't seem like paintEvent is called during the dragging. It is called after the dragging has stopped.
Here is the events I have implemented and their placement in the class. The tree view itself lies in a window.
Qt Code:
class Outliner: public QTreeeView { protected: /** mouseMoveEvent() */ { if ( !(event->buttons() & Qt::LeftButton) ) return; if ( (event->pos() - m_qDragStartPosition).manhattanLength() < Application::startDragDistance() ) return; //mimeData->setData ( mimeType, data ); drag->setMimeData ( mimeData ); Qt::DropAction dropAction = drag->start ( Qt::CopyAction | Qt::MoveAction ); repaint ( ); }// mouseMoveEvent(QMouseEvent*) /** mousePressEvent() */ { if ( event->button() == Qt::LeftButton ) m_qDragStartPosition = event->pos ( ); }// mousePressEvent(QMouseEvent*) /** paintEvent() */ { int x, y, w, h; m_rDropSite.getRect ( &x, &y, &w, &h ); painter.drawLine ( 0, y, 192, y ); }// paintEvent(QPaintEvent*) };// class OutlinerTo copy to clipboard, switch view to plain text mode
Ok, I have managed to make the tree view to redraw itself during a dragMoveEvent().
Right now I'm not using the mouseMoveEvent() and mousePressEvent() functions I posted above, instead I use dragMoveEvent() and the posted paintEvent().
dragMouseEvent looks like this:
Qt Code:
{ // a QRect member variable telling where the drag event is currently localted, used by paintEvent(). m_rDropSite = event->answerRect ( ); // TreeModel is my custom QAbstractItemModel TreeModel *tree = static_cast<TreeModel*> ( model() ); // look below for this one tree->emit_layoutChanged ( ); }// dragMoveEvent(QDragMoveEvent*)To copy to clipboard, switch view to plain text mode
This is the thing I feel is a bit bizarr... It seems to be the only way to update
the tree view during a drag operation. This lies in the custom model, class TreeModel, for the view.
Is it ok to do like this?
Qt Code:
/** Public member function in custom model, class TreeModel. The only thing this does is * emit the signal layoutChanged(), forcing the tree view to execute paintEvent. */ void TreeModel::emit_layoutChanged ( ) { emit layoutChanged ( ); }// emit_layoutChanged()To copy to clipboard, switch view to plain text mode
anyway, thanks for the tips.
/pir
Last edited by pir; 20th May 2006 at 14:33.
Hi!
Now I'm stuck again. Thought that it would be best to post this question in this thread, since it puts this question in context.
Now I want the drawn line to either be drawn between the items or in the middle of one. The reason for this is to make it easier for the user to see if the drop site will place the dragged item between two items or inside one. To do this I need to know the coordinates for the rendered items.
So the question is:
How do I get a describtion of the geometrity for a item in my QTreeView? That is, where in the view the item specified is draw defined in view pixel coordinates.
hope somebody knows how to do this or if it's possible...
pir
But maybe this will be better for you:QRect QTreeView::visualRect ( const QModelIndex & index ) const [virtual]
Returns the rectangle on the viewport occupied by the item at index. If the index is not visible or explicitly hidden, the returned rectangle is invalid.
Reimplemented from QAbstractItemView.
QModelIndex QAbstractItemView::indexAt ( const QPoint & point ) const [pure virtual]
Returns the model index of the item at point point.
In the base class this is a pure virtual function.
pir (20th May 2006)
Thanks!
I should check my sight. Have stared at the QTreeView functions and didn't see that function...
/pir
Sometimes you just have to know the function (or at least know that it exists) to spot it in the documentation.
Bookmarks