Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Drag and drop a file after a first file



  • I actually try to use drag and drop.
    But With my code, I can open one file, and later, when I want to drag and drop another file, my drag and drop method doesn't work.

    void window::dragEnterEvent(QDragEnterEvent *event)
    {
        qDebug("New event :");
        if (event->mimeData()->hasUrls()) {
            event->acceptProposedAction();
            qDebug("event accept.");
        } else {
            event->ignore();
            qDebug("event refused.");
        }
    }
    
    void window::dropEvent(QDropEvent *event)
    {
        foreach(const QUrl &url, event->mimeData()->urls())
            open_file(url.toLocalFile());
        event->accept();
    }
    

    I have just these two functions for my drag and drop method. The output is :
    New event : --> for the first file
    event accepted.
    And nothing for the second file after loaded the first.

    Could someone help me?

    PS: Sorry for my English, I'm french.



  • @SGaist said in Drag and drop a file after a first file:

    Nothing obvious comes to mind reading your code.

    Ok ok thanks


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ?
    What Os are you running ?



  • hi @SGaist
    thanks for your answer.
    I'm running in Windows 10 and I am using Qt 5.11.1


  • Lifetime Qt Champion

    Can you show your complete widget code ?



  • main.cpp

    #include "logiciel.h"
    
    void open_file(QString path);
    
    void window::dragEnterEvent(QDragEnterEvent *event)
    {
        qDebug("New event :");
        if (event->mimeData()->hasUrls()) {
            event->acceptProposedAction();
            qDebug("event accept.");
        } else {
            event->ignore();
            qDebug("event refused.");
        }
    }
    
    void window::dropEvent(QDropEvent *event)
    {
        foreach(const QUrl &url, event->mimeData()->urls())
            open_file(url.toLocalFile());
        event->accept();
    }
    
    window::window(std::string version_number)
    {
        std::string title = "Text Editor " + version_number;
        QWidget *main_window = new QWidget;
        setCentralWidget(main_window);
    
        infoLabel = new QLabel(tr("<i>Create, open or<br/>drag and drop a file.</i>"));
        infoLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
        infoLabel->setAlignment(Qt::AlignCenter);
    
        QVBoxLayout *layout = new QVBoxLayout;
        layout->setMargin(5);
        layout->addWidget(infoLabel);
        main_window->setLayout(layout);
    
        createActions();
        createMenus();
    
        text = NULL;
    
        statusBar()->showMessage("");
        setWindowTitle(tr(title.c_str()));
        setMinimumSize(200, 200);
        resize(1080, 960);
        setAcceptDrops(true);
    }
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        window window("1.3");
        window.show();
        return app.exec();
    }
    

    window.h

    #ifndef WINDOW_H
    #define WINDOW_H
    
    #include <QtWidgets>
    #include <text.h>
    
    class window : public QMainWindow
    {
        Q_OBJECT
    
        public:
        window(std::string version_number);
        QAction *create_action_for_this_menu(char *name_action, QMenu *menu);
    
        public slots:
        //file menu
        int create_new_file(void);
        void open_an_empty_file(void);
        void save_file(void);
        void save_file_as(void);
        //file edit
        void left_alignment(void);
        void right_alignment(void);
        void justify_alignment(void);
        void center_alignment(void);
        void undo(void);
        void redo(void);
        void cut(void);
        void copy(void);
        void paste(void);
        void about(void);
        void put_text_in_bold(bool ok);
        void put_text_in_italic(bool ok);
        void put_text_in_underline(bool ok);
        void open_color();
    
        protected:
        void dragEnterEvent(QDragEnterEvent *event);
        void dropEvent(QDropEvent *event);
    
        private:
        //functions
        void open_file(QString path);
        void createActions();
        void createMenus();
        //variables
        text_t *text;
        QMenu *fileMenu;
        QMenu *saveMenu;
        QMenu *editMenu;
        QMenu *formatMenu;
        QMenu *helpMenu;
        QActionGroup *alignmentGroup;
        QActionGroup *languageGroup;
        QAction *newAct;
        QAction *openAct;
        QAction *saveAct;
        QAction *saveAsAct;
        QAction *exitAct;
        QAction *undoAct;
        QAction *redoAct;
        QAction *cutAct;
        QAction *copyAct;
        QAction *pasteAct;
        QAction *boldAct;
        QAction *italicAct;
        QAction *underlineAct;
        QAction *colorAct;
        QAction *leftAlignAct;
        QAction *rightAlignAct;
        QAction *justifyAct;
        QAction *centerAct;
        QAction *aboutAct;
        QAction *aboutQtAct;
        QAction *frenchAct;
        QAction *englishAct;
        QLabel *infoLabel;
    };
    
    #endif
    

    Remains are not very usefull for my problem I think


  • Lifetime Qt Champion

    I don't have a Windows at hand, but I can confirm that the mouse logic works on macOS (with a few minor correction as your code can't be built as is).



  • @SGaist ok, but do you see something who can explain why drag and drop doesn't work after one file drop ?


  • Lifetime Qt Champion

    Nothing obvious comes to mind reading your code.



  • @SGaist said in Drag and drop a file after a first file:

    Nothing obvious comes to mind reading your code.

    Ok ok thanks


Log in to reply