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>
Q_OBJECT
public:
DropTableWidget
(QWidget *parent
= 0);
public slots:
void clear();
signals:
protected:
private:
};
#endif
#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
To copy to clipboard, switch view to plain text mode
fab_droptablewidget.cpp:
#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();
}
#include <QtGui>
#include "droptablewidget.h"
DropTableWidget::DropTableWidget(QWidget *parent) : QTableWidget(parent) {
//set widget default properties:
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
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();
}
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!
Bookmarks