Here's the heeder and source code.
#ifndef DROPAREA_H
#define DROPAREA_H
#include <QTableWidget>
{
Q_OBJECT
public:
QDropTableWidget
(QWidget *parent
= 0);
public slots:
void clear();
signals:
protected:
private:
};
#endif
#ifndef DROPAREA_H
#define DROPAREA_H
#include <QTableWidget>
class QMimeData;
class QDropTableWidget : public QTableWidget
{
Q_OBJECT
public:
QDropTableWidget(QWidget *parent = 0);
public slots:
void clear();
signals:
void changed(const QMimeData *mimeData = 0);
void dropped(const QMimeData *mimeData = 0);
void test(const QMimeData *mimeData);
void OnDrop(QStringList list, int row);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
void dropEvent(QDropEvent *event);
void resizeEvent(QResizeEvent *event) override;
private:
QTableWidget *tablewidget;
};
#endif
To copy to clipboard, switch view to plain text mode
Source:
#include <QtGui>
#include "qdroptablewidget.h"
#include <qheaderview.h>
{
//set widget default properties:
setAlternatingRowColors(true);
setShowGrid(true);
setWordWrap(false);
vh->setDefaultSectionSize(20);
//setUpdatesEnabled( true ) ;
}
event->acceptProposedAction();
emit changed(event->mimeData());
}
event->acceptProposedAction();
}
void QDropTableWidget
::dropEvent(QDropEvent *event
) {
event->acceptProposedAction();
int row = this->rowAt(event->pos().y());
if (event->mimeData()->hasUrls())
{
foreach
(QUrl url, event
->mimeData
()->urls
()) {
printf("%s\n",url.toString().toStdString().c_str());
list.push_back(url.toLocalFile());
}
}
//emit dropped(event->mimeData());
emit OnDrop(list, row);
//emit (event->mimeData());
}
{
event->accept();
}
void QDropTableWidget::clear()
{
emit changed();
}
{
viewport()->update();
viewport()->repaint();
}
#include <QtGui>
#include "qdroptablewidget.h"
#include <qheaderview.h>
QDropTableWidget::QDropTableWidget(QWidget *parent) : QTableWidget(parent)
{
//set widget default properties:
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
setEditTriggers(QAbstractItemView::NoEditTriggers);
setDragDropMode(QAbstractItemView::DropOnly);
setAlternatingRowColors(true);
setShowGrid(true);
setWordWrap(false);
QHeaderView *vh = this->verticalHeader();
vh->setSectionResizeMode(QHeaderView::Fixed);
vh->setDefaultSectionSize(20);
setSelectionBehavior(QAbstractItemView::SelectRows);
//setUpdatesEnabled( true ) ;
}
void QDropTableWidget::dragEnterEvent(QDragEnterEvent *event) {
event->acceptProposedAction();
emit changed(event->mimeData());
}
void QDropTableWidget::dragMoveEvent(QDragMoveEvent *event) {
event->acceptProposedAction();
}
void QDropTableWidget::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
int row = this->rowAt(event->pos().y());
QStringList list;
if (event->mimeData()->hasUrls())
{
foreach (QUrl url, event->mimeData()->urls())
{
printf("%s\n",url.toString().toStdString().c_str());
list.push_back(url.toLocalFile());
}
}
//emit dropped(event->mimeData());
emit OnDrop(list, row);
//emit (event->mimeData());
}
void QDropTableWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
event->accept();
}
void QDropTableWidget::clear()
{
emit changed();
}
void QDropTableWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
viewport()->update();
viewport()->repaint();
}
To copy to clipboard, switch view to plain text mode
Bookmarks