문제

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