Drop files to the mainWindow (QDeclarativeView)
-
hi,
I found Similar discussions in forum
"http://developer.qt.nokia.com/forums/viewthread/913":http://developer.qt.nokia.com/forums/viewthread/913
but No answer.here is my code
main.cpp
@
#include <QtGui/QApplication>
#include "myviewer.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);MyViewer viewer; viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/DnD/main.qml")); viewer.showExpanded(); return app.exec();
}
@myviewer.h
@
#ifndef MYVIEWER_H
#define MYVIEWER_H#include "qmlapplicationviewer.h"
QT_BEGIN_NAMESPACE
class QDragEnterEvent;
class QDropEvent;
QT_END_NAMESPACEclass MyViewer : public QmlApplicationViewer
{
public:
MyViewer(QWidget *parent = 0);protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);};
#endif // MYVIEWER_H
@myviewer.cpp
@
#include <QtGui>
#include "myviewer.h"MyViewer::MyViewer(QWidget *parent)
: QmlApplicationViewer(parent)
{
setAcceptDrops(true);
}void MyViewer::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}void MyViewer::dropEvent(QDropEvent *event)
{}
@can't drop a file from outside, How do I drop files into the QDeclarativeView ?
THX
-
[quote author="Gerolf" date="1311710917"]QDeclarativeView is a QScrollArea. So I think you also have to set the setAcceptDrops on the viewport. and have an event filter on the view port for the drag/drop events.
[/quote]
Thanks Gerolf.
i add code
in main.cpp
@viewer.viewport()->setAcceptDrops(true);@in myviewer.cpp
@void QAbstractScrollArea::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}@but still not work.
-
No I can't, my code is closed source for my boss, sorry.
But you can have a look at this wiki page: "Drag and Drop of files":http://developer.qt.nokia.com/wiki/Drag_and_Drop_of_files
-
Really thanks, Gerolf.
but the key of my question is QDeclarativeView
I use QML , so my Window is Inherited from QDeclarativeView , below is the Inherrited tree :
MyView <---------- I work here
┗QmlApplicationViewer <-----------Qt Creator wizard generated
┗QDeclarativeView <----------- must inherited form this class, because use QML
┗QGraphicsView
┗QAbstractScrollArea
┗QFrame
┗QWidget <-------- if I inherited from QWidget direct , drop OK!
┗QObjectWhy?
-
That's not 100% correct
@
MyView <————— I work here
┗QmlApplicationViewer <—————-Qt Creator wizard generated
┗QDeclarativeView <—————- must inherited form this class, because use QML
┗QGraphicsView
┗QAbstractScrollArea
┗QFrame
┗QWidget <———— if I inherited from QWidget direct , drop OK!
┗QObject
┗> content QWidget as viewport <-- here the drop events will come to!!
@if MyView is your main window then set drops on viewport and myView and use an event filter on the view port of the scroll area, which will receive the drop events.
-
I tried installEventFilter , my code here
@
MyView::MyView(QWidget *parent)
: QmlApplicationViewer(parent)
{setAcceptDrops(true); this->viewport()->setAcceptDrops(true); this->viewport()->installEventFilter(this);
}
bool MyView::eventFilter(QObject *obj, QEvent *event)
{
if (obj == this->viewport() ) {
if (event->type() == QEvent::DragEnter) {
QDragEnterEvent enterEvent = static_cast<QDragEnterEvent>(event);
qDebug() << "enter" ;
enterEvent->accept();return true; } else { return false; } } else { return QmlApplicationViewer::eventFilter(obj, event); }
}
@I understand this Method can intercept all the event . Thus Catch the DragEnter event.
but my Problem is not catch event . first posted code Already can catch DragEnter event, but MyView not accept drag, Mouse display a forbidden cursor .
when used installEventFilter , the problem Still......................
-
[quote author="hoozi" date="1311771920"]but my Problem is not catch event . first posted code Already can catch DragEnter event, but MyView not accept drag, Mouse display a forbidden cursor.
[/quote]Ah, no we come closer to the problem.
You have to accept the drag/drop operation in dragEnter:@
void DocumentWindow::dragEnterEvent(QDragEnterEvent* event)
{
// if some actions should not be usable, like move, this code must be adopted
event->acceptProposedAction();
}
@ -
Hi, I looked a bit into this and also on the internet. Seems like in QDeclarativeView, dropping from outside does not work anymore, "see this thread":http://developer.qt.nokia.com/forums/viewthread/913
You can have a look at "this code fragements,":http://bytebucket.org/gregschlom/qml-drag-drop/src/0d430d66ceb6 perhaps they help you