PDA

View Full Version : Drag 'n Drop problem



kiker99
16th January 2006, 00:15
Hey guys,

at first: thanks a lot for the good work, I fully support the step to build up a new site :) - looks awesome :D

I posted this message a few hours ago at qtforum.org, when i hadn't read what was going on. So i hope this cross-post is ok ;)

I've got a strange problem with drag 'n drop in my app. It works if you drag the label for the first time, but afterwoods that "drag symbol"(little rect) just remains at the starting position, no matter where you move the mouse. The mouse cursor shows then that a drop isn't permitted (everywhere). The TemplateWidget then also doesn't get any dropevents anymore. I've got the same issue in another Dialog, but I use different Widgets there.

Here's my code (using QT4), i wrote it after after having a look at the QT-Examples. QDragLabel is the Widget where the drag begins, it should end in a QTemplateWidget.



void QDragLabel::mousePressEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
return;

QDrag drag(this);

QMimeData *mimeData = new QMimeData;

mimeData->setText(m_text);
drag.setMimeData(mimeData);

drag.start(Qt::CopyAction);
event->accept();
}

void QTemplateWidget::dragEnterEvent (QDragEnterEvent *event)
{
event->accept();
}

void QTemplateWidget::dropEvent ( QDropEvent * event )
{
bool ok;
QString text;

if(!event->mimeData()->hasText())
{
event->ignore();
return;
}

event->acceptProposedAction();
if(event->mimeData()->text()=="Textfeld")
{
while(true)
{
text = QInputDialog::getText(this, "Bitte geben sie den Namen für das Textfeld an:",
"Name:", QLineEdit::Normal,
"", &ok);
if (ok && !text.isEmpty())
break;
}
QFontMetrics fontmetrics=QApplication::fontMetrics();
QRect rect=fontmetrics.boundingRect(text);
int x=event->pos().x()-rect.width()/2;
int y=event->pos().y()-rect.height()/2;
m_myLabelsX.append(x);
m_myLabelsY.append(y);

m_myLabelsText.append(text);
}
update();
}


Thanks in advance,
kiker99

wysota
16th January 2006, 10:33
QDrag object should be created on heap and not on stack. (So QDrag *drag = ...)

And you should probably use some more standard way to check if you should accept the drop :)

kiker99
16th January 2006, 14:49
Thanks a lot :)
it works. Never thought about that could be the cause...
You're also right with the checking issue, it would be best if the app would only support drags from QDragLabel. Perhaps I should create my own MimeType?

wysota
16th January 2006, 15:27
I meant this:

void QTemplateWidget::dragEnterEvent (QDragEnterEvent *event)
{
event->accept();
}

and


if(!event->mimeData()->hasText())
{
event->ignore();
return;
}

The docs suggest:


void Window::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain"))
event->acceptProposedAction();
}

You shouldn't accept every type that exists like you do.

kiker99
16th January 2006, 17:35
yeah, thanks. :)