[Interest] Getting QTreeView item click offset for DnD

Bill Crocker william.crocker at analog.com
Wed Apr 18 17:54:31 CEST 2012


On 04/18/2012 10:54 AM, Jason H wrote:
> There is no drag start event.
>

If you are dragging then there was a drag start event
and there is an active QDrag object somewhere.

You will need to dig into the source code and find
out how a QTreeView widget supports D&D.

You will then have to hope that all or part of it
can be replaced with your code which takes control
of the creation of the dragged image and corresponding
hotspot.

There is almost no problem with Qt which can not be solved
by studying thousands of lines of Qt source coda and then
writing another thousand of your own.

:-)

Bill

> I attempted to copy what they did in an example:
>
>   void  DragWidget::mousePressEvent(QMouseEvent  <qmouseevent.html>  *event)
>   {
>       QLabel  <qlabel.html>  *child=  static_cast<QLabel  <qlabel.html>*>(childAt(event->pos()));
>       if  (!child)
>           return;
>
>       QPixmap  <qpixmap.html>  pixmap=  *child->pixmap();
>
>       QByteArray  <qbytearray.html>  itemData;
>       QDataStream  <qdatastream.html>  dataStream(&itemData,  QIODevice  <qiodevice.html>::WriteOnly);
>       dataStream<<  pixmap<<  QPoint  <qpoint.html>(event->pos()-  child->pos());
>
>       QMimeData  <qmimedata.html>  *mimeData=  new  QMimeData  <qmimedata.html>;
>       mimeData->setData("application/x-dnditemdata",  itemData);
>
>       QDrag  <qdrag.html>  *drag=  new  QDrag  <qdrag.html>(this);
>       drag->setMimeData(mimeData);
>       drag->setPixmap(pixmap);
>       drag->setHotSpot(event->pos()-  child->pos());
>
>       QPixmap  <qpixmap.html>  tempPixmap=  pixmap;
>       QPainter  <qpainter.html>  painter;
>       painter.begin(&tempPixmap);
>       painter.fillRect(pixmap.rect(),  QColor  <qcolor.html>(127,  127,  127,  127));
>       painter.end();
>
>       child->setPixmap(tempPixmap);
>
>       if  (drag->exec(Qt  <qt.html>::CopyAction|  Qt  <qt.html>::MoveAction,  Qt  <qt.html>::CopyAction)==  Qt  <qt.html>::MoveAction)
>           child->close();
>       else  {
>           child->show();
>           child->setPixmap(pixmap);
>       }
>   }
>
>
> However this wont work because I am using a QTreeView which uses item delegates.
> So the following won't work:
> QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
> drag->setHotSpot(event->pos() - child->pos())
>
> Because QTreeView uses delegates. I need some way to find out where in the
> delegate the mouse was clicked.
>
> ________________________________
> From: Bill Crocker <william.crocker at analog.com>
> To:
> Cc: "interest at qt-project.org" <interest at qt-project.org>
> Sent: Wednesday, April 18, 2012 10:46 AMtake control of the creation of the dragged image relative
> to the Drag-Start pos().
> Subject: Re: [Interest] Getting QTreeView item click offset for DnD
>
> On 04/18/2012 10:33 AM, Jason H wrote:
>>  That is wrong, because that is the mouse position of the drop location.
>>
>>  Here, let me make a picture
>>  +--------------+
>>  |---deleg.te---|
>>  +--------------+
>>
>>  Now the "." in the delegate is the mouse location. When I drop this delegate on
>>  the widget, I get the location of the "." in the widget coordinates. The problem
>>  is, if I put an item at that location it is off by
>>  +--------
>>  |---deleg
>>
>>  So I need to subtract (10,2 - in characters) from the "." position(10,2) to get
>>  the proper place in the widget to position the item. This is the offset I am
>>  looking for.
>>
>
> Perhaps you need to take control of the Drag-Start event and
> there-by take control of the creation of the dragged image relative
> to the Drag-Start pos().
>
> Perhaps the information you seek is buried in the mime data
> provided as part of the drop event.
>
>>
>>
>>  --------------------------------------------------------------------------------
>>  *From:* Bill Crocker <william.crocker at analog.com>
>>  *To:* interest at qt-project.org
>>  *Sent:* Wednesday, April 18, 2012 10:17 AM
>>  *Subject:* Re: [Interest] Getting QTreeView item click offset for DnD
>>
>>  On 04/18/2012 10:07 AM, Jason H wrote:
>> > Please, any help?
>> >
>> > I'm stuck.
>> >
>>
>>  How about this:
>>
>>  const QPoint & QDropEvent::pos () const
>>
>>  Returns the position where the drop was made.
>>
>>
>> > --------------------------------------------------------------------------------
>> > *From:* Jason H <scorp1us at yahoo.com <mailto:scorp1us at yahoo.com>>
>> > *To:* André Somers <andre at familiesomers.nl <mailto:andre at familiesomers.nl>>;
>>  "interest at qt-project.org <mailto:interest at qt-project.org>"
>> > <interest at qt-project.org <mailto:interest at qt-project.org>>
>> > *Sent:* Tuesday, April 17, 2012 11:09 AM
>> > *Subject:* Re: [Interest] Getting QTreeView item click offset for DnD
>> >
>> > Yes. However nothing gives me the click coordinates.
>> >
>> > I am dragging an item to a subclassed QLabel. When the drop occurs, the top-left
>> > of the item is placed at the mouse cursor. This is wrong. The item should be
>> > placed at wherever Qt drew it last, which is offset by the mouse coords in the
>> > item.
>> >
>> >
>> > --------------------------------------------------------------------------------
>> > *From:* André Somers <andre at familiesomers.nl <mailto:andre at familiesomers.nl>>
>> > *To:* "interest at qt-project.org <mailto:interest at qt-project.org>"
>>  <interest at qt-project.org <mailto:interest at qt-project.org>>
>> > *Sent:* Tuesday, April 17, 2012 11:00 AM
>> > *Subject:* Re: [Interest] Getting QTreeView item click offset for DnD
>> >
>> > Op 17-4-2012 16:54, Jason H schreef:
>> > > Well I am confused about how the view works.
>> > > I'm using QStandardItems, so whatever Qt does with those... I can't find
>> > > documentation anywhere...
>> > >
>> > The items in the model do not become widgets. That would be very inefficient.
>> > Instead, they are rendered in the view using a delegate that has been set on
>> > your view. By default, that is a QStyledItemDelegate.
>> >
>> > Anyway, for D&D purposes, an item is just a package of data. At the moment you
>> > have the model index, you have access to the data of the item.
>> >
>> > Perhaps the question should be: why are you reimplementing the mousePressEvent
>> > in this context at all? Did you study the "Using Drag and Drop with Item Views"
>> > topic from the Model/View Programming documentation page?
>> >
>> > André
>> >
>> >
>> > >
>> > >
> --------------------------------------------------------------------------------
>> > > *From:* André Somers <andre at familiesomers.nl <mailto:andre at familiesomers.nl>>
>>  <mailto:andre at familiesomers.nl <mailto:andre at familiesomers.nl>>
>> > > *To:* interest at qt-project.org <mailto:interest at qt-project.org>
>>  <mailto:interest at qt-project.org <mailto:interest at qt-project.org>>
>> > > *Sent:* Tuesday, April 17, 2012 10:40 AM
>> > > *Subject:* Re: [Interest] Getting QTreeView item click offset for DnD
>> > >
>> > > Op 17-4-2012 16:35, Jason H schreef:
>> > >> I need to repeat this. I'm still lost.
>> > >> here is my code (child is always NULL):
>> > >> voidDataPointTreeView::mousePressEvent(QMouseEvent*event)
>> > >> {
>> > >> QTreeView::mousePressEvent(event);
>> > >> QModelIndex mi = indexAt(event->pos());
>> > >> QWidget *child = indexWidget(mi);
>> > >> //QWidget *child = static_cast<QWidget*>(childAt(event->pos()));
>> > >> if (!child)
>> > >> return;
>> > >>
>> > >> m_dragOffset = event->pos() - child->pos();
>> > >> }
>> > >>
>> > > Are you even using widgets for your items? I mean: do you call setIndexWidget
>> > > anywhere? If not, perhaps you should check what indexWidget() actually returns?
>> >
>> >
>> > _______________________________________________
>> > Interest mailing list
>> > Interest at qt-project.org <mailto:Interest at qt-project.org>
>>  <mailto:Interest at qt-project.org <mailto:Interest at qt-project.org>>
>> > http://lists.qt-project.org/mailman/listinfo/interest
>> >
>> >
>> >
>> > _______________________________________________
>> > Interest mailing list
>> > Interest at qt-project.org <mailto:Interest at qt-project.org>
>>  <mailto:Interest at qt-project.org <mailto:Interest at qt-project.org>>
>> > http://lists.qt-project.org/mailman/listinfo/interest
>> >
>> >
>>
>>
>>  _______________________________________________
>>  Interest mailing list
>>  Interest at qt-project.org <mailto:Interest at qt-project.org>
>>  http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest





More information about the Interest mailing list