[Qt-interest] Drag-n-Drop only works the first time
Michael Jackson
mike.jackson at bluequartz.net
Mon Apr 5 21:10:33 CEST 2010
I have created a subclass of QGraphicsView in order to allow the user to
drop a file onto the QGraphicsView widget and have the widget display the
contents of the image file. The image loading code is in another class so I
just figured I would hook everything up with Signals/Slots. The problem is
taht the first drop works just fine. Subsequent drags from the file system
onto the QGraphicsView widget fail to even show the "drag-n-drop" icon and
in fact does not seem to accept the drop. If I omit the "emit
loadImageFileRequested()" line of code then I can perform multiple drops
onto the QGraphicsView widget without problems, of course now that I
commented out the loading code nothing actually loads.)
My immediate question would be are there issues with emitting a signal from
the DropEvent(....) method that I have implemented?
Here is the header:
class QFSDroppableGraphicsView : public QGraphicsView {
Q_OBJECT
public:
QFSDroppableGraphicsView(QWidget *parent = NULL);
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
signals:
void loadImageFileRequested(const QString &filename);
};
Here is the implementation:
void QFSDroppableGraphicsView::dropEvent(QDropEvent *event)
{
QList<QUrl> urlList;
QString fName;
QFileInfo info;
if (event->mimeData()->hasUrls())
{
urlList = event->mimeData()->urls(); // returns list of QUrls
// if just text was dropped, urlList is empty (size == 0)
if ( urlList.size() > 0) // if at least one QUrl is present in list
{
fName = urlList[0].toLocalFile(); // convert first QUrl to local path
info.setFile( fName ); // information about file
QString ext = info.suffix();
if (ext.compare("tif") == 0
|| ext.compare("jpg") == 0
|| ext.compare("png") == 0
|| ext.compare("bmp") == 0)
{
emit loadImageFileRequested(fName);
}
}
}
}
The signals and slots are getting hooked up correctly as I can debug through
the stack for the first drop.
Any help would be greatly appreciated.
--
Mike Jackson
Principal Software Engineer
BlueQuartz Consulting
More information about the Qt-interest-old
mailing list