QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit
-
The code is shown below:
void ConsoleEdit::dropEvent(QDropEvent *e) { QFileInfo file(e->mimeData()->text()); if(file.suffix().contains("Bin") ||\ file.suffix().contains("bin") ||\ file.suffix().contains("BIN")){ qDebug("support file\r\n"); if(pFile->isOpen()){ pFile->close(); } else{ pFile->setFileName(e->mimeData()->urls().first().toLocalFile()); if(pFile->open(QIODevice::ReadOnly)){ emit newBinFileDragInEvent(); //appendInfo("New file opened\r\n"); } } } else{ appendInfo("Unsupport file .\r\n", INFO_TYPE_ERR); } QTextEdit::dropEvent(e); }
-
@Soul
something like this:bool MyTextEdit::canInsertFromMimeData(const QMimeData *source) const { return source->hasUrls() || QTextEdit::canInsertFromMimeData(source); } void MyTextEdit::insertFromMimeData(const QMimeData *source) { if (source->hasUrls()) { QFile file (source->urls().first().toLocalFile()); if ( file.open(QIODevice::ReadOnly) ) { QTextStream in( &file ); this->setPlainText( in.readAll() ); return; } } QTextEdit::insertFromMimeData(source); }
-
@raven-worx
Hi Raven, Thanks for you replying.
I tryed the code you had given to me. But I think it still can not prevent the dropEvent insert "file:///XXXX" to the end.
Can I pass something NULL-Event to the QTextEdit::dropEvent() ? -
@Soul said in QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit:
QTextEdit::dropEvent(e);
I would guess the default implementation just reads the url from the QMimeData and appends it to the QTextEdit. Try to simply not call the base implementation to avoid this.
-
@Soul said in QTextEdit's dropEvent always append "file:///XXXXXXXXXXXX" to the end of QTextEdit:
But I think it still can not prevent the dropEvent insert "file:///XXXX" to the end.
do you think or do you know?
I haven't tested my code, but if there are urls in the mime data they are inserted before any other data (like text). -
@Christian-Ehrlicher Yes it's the reason. But when I do not call the QTextEdit::dropEvent(e), the text cursor is frozened after I drag the file into text area.