Results 1 to 3 of 3

Thread: QMainWindow drag and drop in Qt 5.11.1

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default QMainWindow drag and drop in Qt 5.11.1

    I cannot get drag and drop to work under Qt 5.11.1 x64 on Windows 10, VS 2015. Here's an absolute minimum example. The dummy variables in the event handlers are just to allow me to set breakpoints. None of the drag or drop event handlers is ever hit, when for example, dragging a file from Windows Explorer onto the app.

    Anyone know of a problem with this?

    Edit: Same behavior if I replace QMainWindow with QDialog or QWidget as the base class.

    Qt Code:
    1. // dragdroptest.h
    2.  
    3. #ifndef DRAGDROPTEST_H
    4. #define DRAGDROPTEST_H
    5.  
    6. #include <QtWidgets/QMainWindow>
    7.  
    8. class DragDropTest : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. DragDropTest(QWidget *parent = 0);
    14. ~DragDropTest();
    15.  
    16. protected:
    17. void dragEnterEvent( QDragEnterEvent * pEvent );
    18. void dragMoveEvent( QDragMoveEvent * pEvent );
    19. void dragLeaveEvent( QDragLeaveEvent * pEvent );
    20. void dropEvent( QDropEvent * pEvent );
    21.  
    22. private:
    23. };
    24.  
    25. #endif // DRAGDROPTEST_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // dragdroptest.cpp
    2.  
    3. #include "dragdroptest.h"
    4.  
    5. DragDropTest::DragDropTest(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. setAcceptDrops( true );
    9.  
    10. // Doesn't matter whether a central widget is set or not.
    11. // setCentralWidget( new QWidget( this ) );
    12. }
    13.  
    14. DragDropTest::~DragDropTest()
    15. {
    16.  
    17. }
    18.  
    19. void DragDropTest::dragEnterEvent( QDragEnterEvent * pEvent )
    20. {
    21. int foo = 42;
    22. }
    23.  
    24. void DragDropTest::dragMoveEvent( QDragMoveEvent * pEvent )
    25. {
    26. int foo = 42;
    27. }
    28.  
    29. void DragDropTest::dragLeaveEvent( QDragLeaveEvent * pEvent )
    30. {
    31. int foo = 42;
    32. }
    33.  
    34. void DragDropTest::dropEvent( QDropEvent * pEvent )
    35. {
    36. int foo = 42;
    37. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // main.cpp
    2.  
    3. #include "dragdroptest.h"
    4. #include <QtWidgets/QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. DragDropTest w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 31st July 2020 at 03:12.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QMainWindow drag and drop in Qt 5.11.1

    I have also tested and confirmed that this does not work under Qt 5.14.2, Windows MSVC 2015 64-bit. I haven't really found anything online that addresses this - dragging from outside the program onto the main window - so any advice on what might be causing the problem would be appreciated.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QMainWindow drag and drop in Qt 5.11.1

    OK, I have found the reason in this stackoverflow post.

    It has nothing to do with Qt and everything to do with Microsoft Windoze stupidity. I start Visual Studio in one of two ways:

    1 - For projects that I am working on all the time, I make a Windows BAT file with a shortcut on my desktop that starts VS and loads the solution.

    2 - For new projects or ones I work on infrequently, I double-click the Visual Studio desktop icon, which opens VS with no solution loaded, the create or load the project from the VS menu.

    In case 1, generally the BAT file starts up VS with ordinary user permissions. I have a program there where drag and drop works completely fine. In case 2, I had the VS shortcut set to "Run as Administrator" because sometimes I have code that needs to write to protected areas or needs Admin permission. It was easier to set Admin permission on the icon and forget it than to have to shut down a project and restart it because you forgot it needed special permissions. That what I used to create the program I posted above, where D&D doesn't work, and that's how I have started it each time for the last two days while I have been trying to figure this out.

    So after reading the stackoverflow post, I changed the startup to run as a normal user and loaded the program. And no surprise, my little test program works just fine.

    For those of you who might come across this post years from now, wondering why D&D works sometimes but not others even on the same program, here's the answer:

    Windows security will not let you drag from an application running with user-level security (like Windows file explorer) and drop onto one that is running at a higher level of security. And if you start Visual Studio (or Qt Creator) with Admin security, then any program you start from inside those IDEs inherits that security and won't allow drag and drop from programs with lower security.

    So thank you Microsoft for wasting two entire days of my life.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 13th November 2012, 15:27
  2. Replies: 2
    Last Post: 25th June 2012, 20:14
  3. Replies: 1
    Last Post: 13th October 2011, 15:59
  4. Replies: 2
    Last Post: 13th October 2010, 22:51
  5. Replies: 0
    Last Post: 4th May 2010, 11:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.