Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Drag and drop a file after a first file
QtWS25 Last Chance

Drag and drop a file after a first file

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
9 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bozo6919
    wrote on last edited by
    #1

    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.

    Sorry for my English ^^'

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bozo6919
      wrote on last edited by
      #9

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

      Nothing obvious comes to mind reading your code.

      Ok ok thanks

      Sorry for my English ^^'

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        B 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

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

          B Offline
          B Offline
          bozo6919
          wrote on last edited by
          #3

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

          Sorry for my English ^^'

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Can you show your complete widget code ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply
            0
            • SGaistS SGaist

              Can you show your complete widget code ?

              B Offline
              B Offline
              bozo6919
              wrote on last edited by
              #5

              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

              Sorry for my English ^^'

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                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).

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                B 1 Reply Last reply
                0
                • SGaistS SGaist

                  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).

                  B Offline
                  B Offline
                  bozo6919
                  wrote on last edited by bozo6919
                  #7

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

                  Sorry for my English ^^'

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Nothing obvious comes to mind reading your code.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      bozo6919
                      wrote on last edited by
                      #9

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

                      Nothing obvious comes to mind reading your code.

                      Ok ok thanks

                      Sorry for my English ^^'

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved