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?
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:
Code:
#ifndef DROPAREA_H
#define DROPAREA_H
#include <QTableWidget>
Q_OBJECT
public:
DropTableWidget
(QWidget *parent
= 0);
public slots:
void clear();
signals:
protected:
private:
};
#endif
fab_droptablewidget.cpp:
Code:
#include <QtGui>
#include "droptablewidget.h"
//set widget default properties:
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setAlternatingRowColors(true);
setShowGrid(false);
setWordWrap(false);
setAcceptDrops(true);
}
event->acceptProposedAction();
emit changed(event->mimeData());
}
event->acceptProposedAction();
}
void DropTableWidget
::dropEvent(QDropEvent *event
) {
event->acceptProposedAction();
emit dropped(event->mimeData());
}
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!
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?
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.