1

为什么在这个 QTextEdit 上拖放图片不起作用?我已经尝试了一切。

这是文本编辑类:

 //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");
    }
}
};

上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文上下文"

4

1 回答 1

1

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 :)

于 2012-01-30T01:54:38.267 回答