PDA

View Full Version : Drag and Drop QTableWidget in UI file.



tpf80
12th December 2008, 05:56
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?

tpf80
19th December 2008, 22:26
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:


#ifndef DROPAREA_H
#define DROPAREA_H

#include <QTableWidget>

class QMimeData;

class DropTableWidget : public QTableWidget {
Q_OBJECT

public:
DropTableWidget(QWidget *parent = 0);

public slots:
void clear();

signals:
void changed(const QMimeData *mimeData = 0);
void dropped(const QMimeData *mimeData = 0);

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

private:
QTableWidget *tablewidget;
};

#endif



fab_droptablewidget.cpp:


#include <QtGui>
#include "droptablewidget.h"

DropTableWidget::DropTableWidget(QWidget *parent) : QTableWidget(parent) {
//set widget default properties:
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setEditTriggers(QAbstractItemView::NoEditTriggers) ;
setDragDropMode(QAbstractItemView::DropOnly);
setAlternatingRowColors(true);
setSelectionMode(QAbstractItemView::NoSelection);
setShowGrid(false);
setWordWrap(false);
setAcceptDrops(true);

}

void DropTableWidget::dragEnterEvent(QDragEnterEvent *event) {

event->acceptProposedAction();
emit changed(event->mimeData());
}

void DropTableWidget::dragMoveEvent(QDragMoveEvent *event) {
event->acceptProposedAction();
}

void DropTableWidget::dropEvent(QDropEvent *event) {

event->acceptProposedAction();
emit dropped(event->mimeData());
}

void DropTableWidget::dragLeaveEvent(QDragLeaveEvent *event) {
event->accept();
}

void DropTableWidget::clear() {
emit changed();
}



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!

Cruz
20th January 2009, 16:21
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?

tpf80
21st January 2009, 00:02
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.