PDA

View Full Version : QTextEdit Drag and Drop



guiQt
21st August 2010, 15:52
First step was:
MainWindow::MainWindow()
{
textEdit = new QTextEdit;
setCentralWidget(textEdit);

textEdit->setAcceptDrops(false);
setAcceptDrops(true);

setWindowTitle(tr("Text Editor"));
}

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

void MainWindow::dragMoveEvent(QDragMoveEvent *event)
{
event->accept();
}

void MainWindow::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("text/uri-list")) {
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;

QString fileName = urls.first().toLocalFile();
if (fileName.isEmpty())
return;

if (readFile(fileName))
setWindowTitle(tr("%1 - %2").arg(fileName)
.arg(tr("Drag File")));
}
}
In the second step I enabled drops for textEdit
textEdit->setAcceptDrops(true) and subclassed it, so now it's possible to move/copy a text from a line to another:

...dragEnterEvent(QDragEnterEvent *e)
{
if (e->source()) {
if (e->source()->objectName() == "qt_scrollarea_viewport") {
e->accept();
}
}
}

...dragMoveEvent(QDragMoveEvent *e)
{
e->accept();
}
My questions:
1. During drag the cursor stays hidden: is this due to the non-propagation of the event to the parent?
2. Why subclassing dropEvent() like this
...dropEvent(QDropEvent *e)
{
e->accept();
}text is not copied/moved?
3. Are there any alternatives to get this task?
I hope this thread will be useful for others.
Thank you so much.

guiQt
22nd August 2010, 09:50
4. Why
...dragEnterEvent(QDragEnterEvent *e)
{
if (e->source() == this)
e->accept();
} does not work?

guiQt
24th August 2010, 07:37
Unclear questions?

guiQt
26th August 2010, 23:41
5. Why
...dropEvent(QDropEvent *e)
{
const QMimeData *mimeData = e->mimeData();
insertFromMimeData(mimeData);
}this neither?

squidge
29th August 2010, 00:02
Please don't ask questions in visitor messages. If someone can answer your post, they will.

guiQt
29th August 2010, 16:36
Thank you for your kind specification and for the precious help.