An extremely simple text editor accepts external file drop changing its title:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
}
{
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;
setWindowTitle(tr("%1 - %2").arg(fileName).arg(tr("Drag File")));
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
}
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;
setWindowTitle(tr("%1 - %2").arg(fileName).arg(tr("Drag File")));
}
}
To copy to clipboard, switch view to plain text mode
MainWindow has just one widget, textEdit, defined by:
MyTextEdit
::MyTextEdit(QWidget *parent
){
setAcceptDrops(true);
}
{
if (e->source()) {
if (e->source() == viewport()) {
e->accept();
}
}
}
MyTextEdit::MyTextEdit(QWidget *parent)
: QTextEdit(parent)
{
setAcceptDrops(true);
}
void MyTextEdit::dragEnterEvent(QDragEnterEvent *e)
{
if (e->source()) {
if (e->source() == viewport()) {
e->accept();
}
}
}
To copy to clipboard, switch view to plain text mode
textEdit accepts only "self" drops, so inserted text can then be moved or copied.
What is the reason of the pointer flickering? How can i remove it?
Thanks.
Bookmarks