Why doesn't drag and drop pictures work on this QTextEdit? I have tried everything.

here is the class TextEdit:

 //textedit
class TextEdit : public QTextEdit
 {
Q_OBJECT
public:




TextEdit(QWidget*parent) : QTextEdit(parent)
{
    this->setAcceptDrops(true);
}


virtual void dragEnterEvent(QDragEnterEvent *e)
{
    e->accept();
    //QTextEdit::dragEnterEvent(e);
}

virtual void dragLeaveEvent(QDragLeaveEvent *e)
{
    e->accept();
    //QTextEdit::dragLeaveEvent(e);
}
//
virtual void dragMoveEvent(QDragMoveEvent *e)
{
    e->accept();
   // QTextEdit::dragMoveEvent(e);
}

virtual void dropEvent(QDropEvent *e)
{
    QTextEdit::dropEvent(e);
}

bool canInsertFromMimeData(const  QMimeData *source ) const
{
    if (source->hasImage())
        return true;
    else
        return QTextEdit::canInsertFromMimeData(source);
}


void insertFromMimeData( const QMimeData *source )
{
    if (source->hasImage())
    {
        QImage image = qvariant_cast<QImage>(source->imageData());
        QTextCursor cursor = this->textCursor();
        QTextDocument *document = this->document();
        document->addResource(QTextDocument::ImageResource, QUrl("image"), image);
        cursor.insertImage("image");
    }
}
};

context context context context context context context context context context context context context context context context context context context context context context context context context context context context context context context context

有帮助吗?

解决方案

It depends on what application you are dragging the images from and what data that application decides to include in the operation. If it is not working for you, it is because whatever you are dropping contains no image data and probably only contains a URL or file path.

Dragging images from the file explorer under Windows 7 for me at least does not work but opening an image in the latest version of Firefox and dragging that onto the text edit does work. Try it :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top