Results 1 to 4 of 4

Thread: Drag and Drop QTableWidget in UI file.

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Drag and Drop QTableWidget in UI file.

    Using Qt 4.4.3

    I have a Qtablewidget which is in a .ui file, and is used to make a list of files. In designer I have enabled drop to this widget, however I am not totally sure what signal is triggered on drop, nor how to get the mime information. My goal is to be able to drag a file from outside of my program on to the QTableWidget which is defined in the .ui file, and have the mime data returned upon that event, so I can do something with it.

    I have looked do far in the forums and on the "Drop Site Example" which seems to be the closest thing to what I need. However, none of the examples that I have found show using drag and drop into a widget that was placed with designer (the interface is getting kindof complex, so being able to keep it all within the .ui file helps a lot).

    How can I enable the QTableWidget within my .ui file to work with a drop from an external source?

  2. #2
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Drag and Drop QTableWidget in UI file.

    well after some trial and error I got it working, and here are the results in case anyone else finds them useful:

    First I enabled a QTableWidget to use drag and drop, and added a signal to emit the mime data when something is dropped on it, going off of the drop site example:

    fab_droptablewidget.h:
    Qt Code:
    1. #ifndef DROPAREA_H
    2. #define DROPAREA_H
    3.  
    4. #include <QTableWidget>
    5.  
    6. class QMimeData;
    7.  
    8. class DropTableWidget : public QTableWidget {
    9. Q_OBJECT
    10.  
    11. public:
    12. DropTableWidget(QWidget *parent = 0);
    13.  
    14. public slots:
    15. void clear();
    16.  
    17. signals:
    18. void changed(const QMimeData *mimeData = 0);
    19. void dropped(const QMimeData *mimeData = 0);
    20.  
    21. protected:
    22. void dragEnterEvent(QDragEnterEvent *event);
    23. void dragMoveEvent(QDragMoveEvent *event);
    24. void dragLeaveEvent(QDragLeaveEvent *event);
    25. void dropEvent(QDropEvent *event);
    26.  
    27. private:
    28. QTableWidget *tablewidget;
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 

    fab_droptablewidget.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "droptablewidget.h"
    3.  
    4. DropTableWidget::DropTableWidget(QWidget *parent) : QTableWidget(parent) {
    5. //set widget default properties:
    6. setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
    7. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    8. setEditTriggers(QAbstractItemView::NoEditTriggers);
    9. setDragDropMode(QAbstractItemView::DropOnly);
    10. setAlternatingRowColors(true);
    11. setSelectionMode(QAbstractItemView::NoSelection);
    12. setShowGrid(false);
    13. setWordWrap(false);
    14. setAcceptDrops(true);
    15.  
    16. }
    17.  
    18. void DropTableWidget::dragEnterEvent(QDragEnterEvent *event) {
    19.  
    20. event->acceptProposedAction();
    21. emit changed(event->mimeData());
    22. }
    23.  
    24. void DropTableWidget::dragMoveEvent(QDragMoveEvent *event) {
    25. event->acceptProposedAction();
    26. }
    27.  
    28. void DropTableWidget::dropEvent(QDropEvent *event) {
    29.  
    30. event->acceptProposedAction();
    31. emit dropped(event->mimeData());
    32. }
    33.  
    34. void DropTableWidget::dragLeaveEvent(QDragLeaveEvent *event) {
    35. event->accept();
    36. }
    37.  
    38. void DropTableWidget::clear() {
    39. emit changed();
    40. }
    To copy to clipboard, switch view to plain text mode 

    I then went into designer and added QTableWidgets in the places where I needed to use the DropTableWidget, right clicked them and selected "promote to", and input "DropTableWidget" as the class name and "fab_droptablewidget.h" as the header file. I then added "fab_droptablewidget.h" and "fab_droptablewidget.cpp" to my .pro file, and now it all works as it should. I now connect the "dropped" signal emitted from the widget, to a slot to handle the mime data, and yay it works!

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop QTableWidget in UI file.

    This is a great way to click your gui together with Designer and still be able to add functionality, such as drag and drop. Now the question is, how to add a layout as well?

    I used the same approach and created a QFrame in Designer which I then promoted to MyFrame. MyFrame is able to handle drop events. The dropped objects should be layed out nicely though, so I hoped I can take care of the layout in Designer too. I added a horizontal layout in Designer to the QFrame, promoted it to MyFrame and started the application. I dragged something on the frame and boom, the application crashed. Investigations show that myFrame->layout() returns NULL. The layout has been canceled. It doesn't matter if I first promote and then add the layout either.

    Any ideas how to accomplish this?

  4. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and Drop QTableWidget in UI file.

    The widget you place in designer that is promoted is only a placeholder for the actual custom widget.

    What you might need to do is add the layout as part of your custom widget's code, and not in designer, so that when your widget replaces the placeholder it will already have the layout in it.

Similar Threads

  1. Drag & Drop rows in a QTableWidget
    By vycke in forum Qt Programming
    Replies: 7
    Last Post: 19th January 2012, 01:01
  2. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  3. Drag and Drop QTableWidget and QTableView
    By scorpion11 in forum Qt Programming
    Replies: 5
    Last Post: 8th April 2008, 10:33
  4. Drag & Drop: delayed file delivery (Win & Mac)
    By Conel in forum Qt Programming
    Replies: 3
    Last Post: 19th September 2007, 17:01
  5. Drag & Drop using QTableWidget
    By Israa in forum Qt Programming
    Replies: 21
    Last Post: 12th April 2007, 20:35

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.