Trying to implement a drop function on a QFrame, help please!
-
In the mainwindow.ui file, the Qframe that I need to enable the drop function is organized as follows:
MainWindow(QMainWindow) -> centralwidget(QWidget) -> stackedWidget(QStackedWidget) -> page3(QWidget) -> frame_12(QFrame) -> frame_17(Qframe).
The last one, frame_17, is the frame where i need to drop zip files. Trying to figure out in internet how to do it, I did this:
/_____________________________________________
dropimages.h#ifndef DROPIMAGES_H #define DROPIMAGES_H #include <QWidget> #include <QFrame> class DropImages : public QFrame { public: explicit DropImages(QWidget *parent = nullptr); protected: void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; }; #endif // DROPIMAGES_H
/_____________________________________________
dropimages.cpp
#include "dropimages.h" #include <QDrag> #include <QDragEnterEvent> #include <QMimeData> DropImages::DropImages(QWidget *parent) : QFrame(parent) { setAcceptDrops(true); } void DropImages::dragEnterEvent(QDragEnterEvent *event) { printf("cccccc\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) event->acceptProposedAction(); } void DropImages::dragMoveEvent(QDragMoveEvent *event) { printf("bbbb\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) { event->acceptProposedAction(); } } void DropImages::dropEvent(QDropEvent *event) { printf("aaaaaa\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) { foreach(QUrl url, event->mimeData()->urls()) { printf(url.toLocalFile().toStdString().c_str()); } } }
/_____________________________________________
mainwindow.cpp
inside class implementation:
DropImages *uploadedImages = new DropImages(ui->frame_17);/_____________________________________________
But nothing is working. Can someone help me please?
-
In the mainwindow.ui file, the Qframe that I need to enable the drop function is organized as follows:
MainWindow(QMainWindow) -> centralwidget(QWidget) -> stackedWidget(QStackedWidget) -> page3(QWidget) -> frame_12(QFrame) -> frame_17(Qframe).
The last one, frame_17, is the frame where i need to drop zip files. Trying to figure out in internet how to do it, I did this:
/_____________________________________________
dropimages.h#ifndef DROPIMAGES_H #define DROPIMAGES_H #include <QWidget> #include <QFrame> class DropImages : public QFrame { public: explicit DropImages(QWidget *parent = nullptr); protected: void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; }; #endif // DROPIMAGES_H
/_____________________________________________
dropimages.cpp
#include "dropimages.h" #include <QDrag> #include <QDragEnterEvent> #include <QMimeData> DropImages::DropImages(QWidget *parent) : QFrame(parent) { setAcceptDrops(true); } void DropImages::dragEnterEvent(QDragEnterEvent *event) { printf("cccccc\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) event->acceptProposedAction(); } void DropImages::dragMoveEvent(QDragMoveEvent *event) { printf("bbbb\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) { event->acceptProposedAction(); } } void DropImages::dropEvent(QDropEvent *event) { printf("aaaaaa\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) { foreach(QUrl url, event->mimeData()->urls()) { printf(url.toLocalFile().toStdString().c_str()); } } }
/_____________________________________________
mainwindow.cpp
inside class implementation:
DropImages *uploadedImages = new DropImages(ui->frame_17);/_____________________________________________
But nothing is working. Can someone help me please?
@fabriciokash said in Trying to implement a drop function on a QFrame, help please!:
But nothing is working
Do you mean none of these methods are hit?
MainWindow(QMainWindow) -> centralwidget(QWidget) -> stackedWidget(QStackedWidget) -> page3(QWidget) -> frame_12(QFrame) -> frame_17(Qframe).
You say
frame_12
is aQFrame
(perhaps created in Designer?). But your class isDropImages
, derived fromQFrame
. So this would be a third frame level? May I suggest you start by, say, setting the background color of yourDropImages
to red so that you can at least it is there and what area it covers? -
@fabriciokash said in Trying to implement a drop function on a QFrame, help please!:
But nothing is working
Do you mean none of these methods are hit?
MainWindow(QMainWindow) -> centralwidget(QWidget) -> stackedWidget(QStackedWidget) -> page3(QWidget) -> frame_12(QFrame) -> frame_17(Qframe).
You say
frame_12
is aQFrame
(perhaps created in Designer?). But your class isDropImages
, derived fromQFrame
. So this would be a third frame level? May I suggest you start by, say, setting the background color of yourDropImages
to red so that you can at least it is there and what area it covers?@JonB said in Trying to implement a drop function on a QFrame, help please!:
Do you mean none of these methods are hit?
You say
frame_12
is aQFrame
(perhaps created in Designer?). But your class isDropImages
, derived fromQFrame
. So this would be a third frame level? May I suggest you start by, say, setting the background color of yourDropImages
to red so that you can at least it is there and what area it covers?Yes, these methods was not working. But i found the enabled area using your suggestion and after tried to drop a file, it works! Thank you! I was trying to drop into a wrong area...
But I found another problem. After setting the background area color, only part of the frame was painted red. This means that only this part of the frame is enabled to drop files. This part is a rectangle in the upper left corner of the frame and reduced in size. While the frame size is (800,200), the red colored part is about (120, 40) size. Can you help me with this problem?
-
@JonB said in Trying to implement a drop function on a QFrame, help please!:
Do you mean none of these methods are hit?
You say
frame_12
is aQFrame
(perhaps created in Designer?). But your class isDropImages
, derived fromQFrame
. So this would be a third frame level? May I suggest you start by, say, setting the background color of yourDropImages
to red so that you can at least it is there and what area it covers?Yes, these methods was not working. But i found the enabled area using your suggestion and after tried to drop a file, it works! Thank you! I was trying to drop into a wrong area...
But I found another problem. After setting the background area color, only part of the frame was painted red. This means that only this part of the frame is enabled to drop files. This part is a rectangle in the upper left corner of the frame and reduced in size. While the frame size is (800,200), the red colored part is about (120, 40) size. Can you help me with this problem?
@fabriciokash
Since the frame is empty (has no content) I would guess it could be any size. I guess you could set a fixed size, or minimum sizes, or presumably best check its sizePolicy, you want it to fill its parent, I don't know what that is for aQFrame
. -
In the mainwindow.ui file, the Qframe that I need to enable the drop function is organized as follows:
MainWindow(QMainWindow) -> centralwidget(QWidget) -> stackedWidget(QStackedWidget) -> page3(QWidget) -> frame_12(QFrame) -> frame_17(Qframe).
The last one, frame_17, is the frame where i need to drop zip files. Trying to figure out in internet how to do it, I did this:
/_____________________________________________
dropimages.h#ifndef DROPIMAGES_H #define DROPIMAGES_H #include <QWidget> #include <QFrame> class DropImages : public QFrame { public: explicit DropImages(QWidget *parent = nullptr); protected: void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; }; #endif // DROPIMAGES_H
/_____________________________________________
dropimages.cpp
#include "dropimages.h" #include <QDrag> #include <QDragEnterEvent> #include <QMimeData> DropImages::DropImages(QWidget *parent) : QFrame(parent) { setAcceptDrops(true); } void DropImages::dragEnterEvent(QDragEnterEvent *event) { printf("cccccc\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) event->acceptProposedAction(); } void DropImages::dragMoveEvent(QDragMoveEvent *event) { printf("bbbb\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) { event->acceptProposedAction(); } } void DropImages::dropEvent(QDropEvent *event) { printf("aaaaaa\n"); // for test event->accept(); if(event->mimeData()->hasUrls()) { foreach(QUrl url, event->mimeData()->urls()) { printf(url.toLocalFile().toStdString().c_str()); } } }
/_____________________________________________
mainwindow.cpp
inside class implementation:
DropImages *uploadedImages = new DropImages(ui->frame_17);/_____________________________________________
But nothing is working. Can someone help me please?
-
Have a look at this topic and my replies there.
It seems that you don't create aQDrag
object which will transfer the mime data.@Pl45m4
I have never done d&d. Your post is forQGraphicsScene
, I presume what you said there aboutQDrag
object is just as relevant here? [You wrote "QDrop
object" above, but you meantQDrag
, right?]I came across Qt's Drop Site Example. Is that relevant for @fabriciokash ? That doesn't seem to use any
QDrag
object, yet it's handling MIME data, is it a different approach from yours? -
Have a look at this topic and my replies there.
It seems that you don't create aQDrag
object which will transfer the mime data.