How to add content of text/sql file in QPlainTextEdit on QDropEvent.
-
in the
CodeEditor, a subclass ofQPlainTextEditI've these:void CodeEditor::dragEnterEvent(QDragEnterEvent *event) { QPlainTextEdit::dragEnterEvent(event); } void CodeEditor::dropEvent(QDropEvent *event) { QFile file (event->mimeData()->text().remove("file:///")); file.open(QIODevice::ReadOnly); QTextStream in(&file); qDebug() << in.readAll(); insertPlainText(in.readAll()); //setPlainText(in.readAll()); file.close(); }when I drop .txt/..sql file on the
CodeEditor, nothing happens:

How to add the content? In addition to that I also want to block drop (and change the cursor icon from copy (+) to block) if the file isn't .txt/.sql .
-
I think the issue is/was that you're calling QTextstream::readAll() twice and there's nothing left to read after the first call. QTextstream's documentation doesn't explicitly say so, but
QIODevice::readAll() says it 'Reads all remaining data from the device'. -
in the
CodeEditor, a subclass ofQPlainTextEditI've these:void CodeEditor::dragEnterEvent(QDragEnterEvent *event) { QPlainTextEdit::dragEnterEvent(event); } void CodeEditor::dropEvent(QDropEvent *event) { QFile file (event->mimeData()->text().remove("file:///")); file.open(QIODevice::ReadOnly); QTextStream in(&file); qDebug() << in.readAll(); insertPlainText(in.readAll()); //setPlainText(in.readAll()); file.close(); }when I drop .txt/..sql file on the
CodeEditor, nothing happens:

How to add the content? In addition to that I also want to block drop (and change the cursor icon from copy (+) to block) if the file isn't .txt/.sql .
@Emon-Haque said in How to add content of text/sql file in QPlainTextEdit on QDropEvent.:
nothing happens
What did you do already to fine out why it does not work?
Please use debugger and add a break point inside dropEvent and go through step by step.
Also check what file.open(QIODevice::ReadOnly) returns. -
Is the widget set to accept drop events? Do you know if anything in your dropEvent() is being run? Was the file open successful?
-
@Emon-Haque said in How to add content of text/sql file in QPlainTextEdit on QDropEvent.:
nothing happens
What did you do already to fine out why it does not work?
Please use debugger and add a break point inside dropEvent and go through step by step.
Also check what file.open(QIODevice::ReadOnly) returns.@jsulm, this
qDebug() << in.readAll()prints out all content in the QtCreator output, I can see content there. -
Is the widget set to accept drop events? Do you know if anything in your dropEvent() is being run? Was the file open successful?
@mchinand, widget that contains the
CodeEditoror the CodeEditor itself? In the constructor of the CodeEditor I've thissetAcceptDrops(true);. Tried the same in the QWidget that contains the CodeEditor, doesn't work. -
@Emon-Haque said in How to add content of text/sql file in QPlainTextEdit on QDropEvent.:
nothing happens
What did you do already to fine out why it does not work?
Please use debugger and add a break point inside dropEvent and go through step by step.
Also check what file.open(QIODevice::ReadOnly) returns.@jsulm, somehow your trick works without any change of code. Just clicked the
Start debugging of startup projectbutton and kept hittingF5and now it works!I always used
qDebugto observe and useCtrl+Rto run the project. Looks like those Play buttons actually have some use. -
I think the issue is/was that you're calling QTextstream::readAll() twice and there's nothing left to read after the first call. QTextstream's documentation doesn't explicitly say so, but
QIODevice::readAll() says it 'Reads all remaining data from the device'. -
I think the issue is/was that you're calling QTextstream::readAll() twice and there's nothing left to read after the first call. QTextstream's documentation doesn't explicitly say so, but
QIODevice::readAll() says it 'Reads all remaining data from the device'.@mchinand, correct. I've tried again with
qDebugand that's the case.
Thanks.