which class need to be addressed to support drag'n drop between Qt app and finder

77 views Asked by At

I'm trying to build an app which list the folders and files from an Android device to finder and be able copy from/to PC.

I have done the class as below in my Browser.cpp

Browser::Browser(USBDevice dev, QWidget *parent) :
     QTreeWidget(parent)
{
    USBDevice MyDevice;

    MyDevice = dev;

    QMainWindow *window = new QMainWindow();

    window->setWindowTitle(QString::fromUtf8("PULS"));
    window->resize(600, 600);
    // window->setDragEnabled(true);
    //   window->setAcceptDrops(true);
    //   window->setMouseTracking(true);



    QWidget *centralWidget = new QWidget(window);

    QPushButton* CreateFolderButton = new QPushButton("Create Folder", centralWidget);
    CreateFolderButton->setFixedSize(100,20);
    //QVBoxLayout* Layout = new QVBoxLayout(centralWidget);
    //Layout->addWidget(CreateFolderButton);

    QPushButton* DeleteButton = new QPushButton("Delete", centralWidget);
    DeleteButton->setFixedSize(100,20);
    QVBoxLayout* Layout = new QVBoxLayout(centralWidget);
    Layout->addWidget(CreateFolderButton);
    Layout->addWidget(DeleteButton);

    QTreeWidget *MyTree = new QTreeWidget(centralWidget);
    Layout->addWidget(MyTree);
    MyTree->setSortingEnabled(true);
    MyTree->setColumnWidth(0, 400);
    MyTree->setDragDropMode(DragDrop);
    MyTree->setDropIndicatorShown(true);
    MyTree->setDragEnabled(true);
    MyTree->setMouseTracking(true);

    QTreeWidgetItem* headerItem = new QTreeWidgetItem();
    headerItem->setText(0,QString("File Name"));
    headerItem->setText(1,QString("Size (Bytes)"));
    headerItem->setText(2,QString("Date"));
    MyTree->setHeaderItem(headerItem);

    Recursive_Parse(MyTree, MyDevice.device_id, MyDevice.device_id->storage, 0, 0, 0);

    window->setCentralWidget(centralWidget);

    MyButtonGroup* group = new MyButtonGroup(MyTree, MyDevice, MyDevice.device_id, MyDevice.device_id->storage, centralWidget);
    group->addButton(CreateFolderButton);
    group->addButton(DeleteButton);

    window->show();
}

and the class in browser.h as

class Browser : public QTreeWidget
{
    Q_OBJECT
    public:
        Browser(USBDevice dev, QWidget* parent = 0);    
        void dragEnterEvent(QDragEnterEvent *event);
        void dropEvent(QDropEvent *event);
        void dragMoveEvent(QDragMoveEvent *event);
        void dragLeaveEvent(QDragLeaveEvent *event);
        ~Browser(){};
};

Drag'n drop is working in the Tree I can move stuff even if it's not working properly as I can drop a file on a file and create a child. By file, I just mean the TreeWidgetItem not the real file. This is also something I need to add but I do not know where.

But I do not see how to handle the dran'ndrop. I need to be able to control the drag'n drop inside the tree and outside to copy file from/to my computer.

It seems that :

        void dragEnterEvent(QDragEnterEvent *event);
        void dropEvent(QDropEvent *event);
        void dragMoveEvent(QDragMoveEvent *event);
        void dragLeaveEvent(QDragLeaveEvent *event);

Are never called... even if the drag'n drop works

Any idea how to handle event and overload or customised the behaviour ?

Thanks

1

There are 1 answers

6
Öö Tiib On

The class that has the dragEnterEvent and stuff overridden is named Browser, however you do not instantiate it, instead you instantiate QTreeWidget:

QTreeWidget *MyTree = new QTreeWidget(centralWidget);

So that is the reason why no members from Browser will get called. You perhaps wanted to write:

QTreeWidget *MyTree = new Browser(MyDevice, centralWidget);

Or something like that.