Skip to content

Commit

Permalink
add drop4
Browse files Browse the repository at this point in the history
  • Loading branch information
leichaojian committed Nov 17, 2014
1 parent 4783d8b commit 5606201
Show file tree
Hide file tree
Showing 8 changed files with 504 additions and 0 deletions.
54 changes: 54 additions & 0 deletions drop4/dialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
listA = new ProjectListWidget;
listB = new ProjectListWidget;
buttonAToB = new QPushButton("->");
buttonBToA = new QPushButton("<-");

listA->addItem("Giosue Carducci");
listA->addItem("Eyvind Johnson");
listA->addItem("Sally Prudhomme");
listA->addItem("Henryk Sienkiewicz");
listA->addItem("Carl Spitteler");
listA->addItem("Rabindranath Tagore");
listA->addItem("Kawabata Yasunari");

listB->addItem("Rudolf Eucken");
listB->addItem("Anatole France");
listB->addItem("Rudyard Kipling");
listB->addItem("Thomas Mann");
listB->addItem("Eugene O'Neill");
listB->addItem("Sigrid Undset");


buttonAToB->setText("->");
buttonBToA->setText("<-");

QVBoxLayout *buttonLayout = new QVBoxLayout;
buttonLayout->addWidget(buttonAToB);
buttonLayout->addWidget(buttonBToA);

QHBoxLayout *hboxLayout = new QHBoxLayout;
hboxLayout->addWidget(listA);
hboxLayout->addLayout(buttonLayout);
hboxLayout->addWidget(listB);
setLayout(hboxLayout);

setFixedHeight( sizeHint().height() );

connect(buttonAToB, SIGNAL(clicked()),
this, SLOT(AToB_clicked()));
connect(buttonBToA, SIGNAL(clicked()),
this, SLOT(BToA_clicked()));
}

Dialog::~Dialog()
{
delete ui;
}
31 changes: 31 additions & 0 deletions drop4/dialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "projectlistwidget.h"

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

private:
Ui::Dialog *ui;
private:
QListWidget *listA;
QListWidget *listB;
QPushButton *buttonAToB;
QPushButton *buttonBToA;
};

#endif // DIALOG_H
20 changes: 20 additions & 0 deletions drop4/dialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>Dialog</string>
</property>
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<resources/>
<connections/>
</ui>
22 changes: 22 additions & 0 deletions drop4/drop4.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#-------------------------------------------------
#
# Project created by QtCreator 2014-11-17T22:50:40
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = drop4
TEMPLATE = app


SOURCES += main.cpp\
dialog.cpp \
projectlistwidget.cpp

HEADERS += dialog.h \
projectlistwidget.h

FORMS += dialog.ui
255 changes: 255 additions & 0 deletions drop4/drop4.pro.user

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions drop4/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();

return a.exec();
}
79 changes: 79 additions & 0 deletions drop4/projectlistwidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "projectlistwidget.h"

ProjectListWidget::ProjectListWidget(QWidget *parent)
: QListWidget(parent)
{
//允许拖放
setAcceptDrops(true);
}

void ProjectListWidget::mousePressEvent(QMouseEvent *event)
{
//获取鼠标按下时候的坐标
if (event->button() == Qt::LeftButton)
startPos = event->pos();
QListWidget::mousePressEvent(event);
}

void ProjectListWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - startPos).manhattanLength();
//如果长度大于推荐的拖动起始距离,则认为是拖动(存在用户手抖的情况)
if (distance >= QApplication::startDragDistance())
performDrag();
}
QListWidget::mouseMoveEvent(event);
}

//有文件拖动到窗口上时,触发此dragEnterEvent事件
void ProjectListWidget::dragEnterEvent(QDragEnterEvent *event)
{
//当为同一个应用程序的一部分时,event->source()返回启动这个拖动窗口部件的指针
ProjectListWidget *source =
qobject_cast<ProjectListWidget *>(event->source());
if (source && source != this) {
//确认是一个移动的操作
event->setDropAction(Qt::MoveAction);
event->accept();
}
}

void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event)
{
ProjectListWidget *source =
qobject_cast<ProjectListWidget *>(event->source());
if (source && source != this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
}

void ProjectListWidget::dropEvent(QDropEvent *event)
{
ProjectListWidget *source =
qobject_cast<ProjectListWidget *>(event->source());
if (source && source != this) {
//得到数据并增加到列表中
addItem(event->mimeData()->text());
event->setDropAction(Qt::MoveAction);
event->accept();
}
}

void ProjectListWidget::performDrag()
{
//当前被拖动的选项
QListWidgetItem *item = currentItem();
if (item) {
QMimeData *mimeData = new QMimeData;
mimeData->setText(item->text());

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(QPixmap(":/images/person.png"));
//进行拖动操作
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
delete item;
}
}
32 changes: 32 additions & 0 deletions drop4/projectlistwidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef PROJECTLISTWIDGET_H
#define PROJECTLISTWIDGET_H

#include <QListWidget>
#include <QApplication>
#include <QMimeData>
#include <QDropEvent>
#include <QDrag>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QMouseEvent>

class ProjectListWidget : public QListWidget
{
Q_OBJECT
public:
ProjectListWidget(QWidget *parent = 0);

protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);

private:
void performDrag();

QPoint startPos;
};

#endif // PROJECTLISTWIDGET_H

0 comments on commit 5606201

Please sign in to comment.