First step was:
MainWindow::MainWindow()
{
setCentralWidget(textEdit);
textEdit->setAcceptDrops(false);
setAcceptDrops(true);
setWindowTitle(tr("Text Editor"));
}
{
event->accept();
}
{
event->accept();
}
{
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")));
}
}
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")));
}
}
To copy to clipboard, switch view to plain text mode
In the second step I enabled drops for textEdit
textEdit->setAcceptDrops(true)
textEdit->setAcceptDrops(true)
To copy to clipboard, switch view to plain text mode
and subclassed it, so now it's possible to move/copy a text from a line to another:
{
if (e->source()) {
if (e->source()->objectName() == "qt_scrollarea_viewport") {
e->accept();
}
}
}
{
e->accept();
}
...dragEnterEvent(QDragEnterEvent *e)
{
if (e->source()) {
if (e->source()->objectName() == "qt_scrollarea_viewport") {
e->accept();
}
}
}
...dragMoveEvent(QDragMoveEvent *e)
{
e->accept();
}
To copy to clipboard, switch view to plain text mode
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
{
e->accept();
}
...dropEvent(QDropEvent *e)
{
e->accept();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks