From 5606201723e96a0765af2d7eda8a7c5ee998cd3e Mon Sep 17 00:00:00 2001 From: leichaojian Date: Mon, 17 Nov 2014 23:05:15 +0800 Subject: [PATCH] add drop4 --- drop4/dialog.cpp | 54 ++++++++ drop4/dialog.h | 31 +++++ drop4/dialog.ui | 20 +++ drop4/drop4.pro | 22 ++++ drop4/drop4.pro.user | 255 ++++++++++++++++++++++++++++++++++++ drop4/main.cpp | 11 ++ drop4/projectlistwidget.cpp | 79 +++++++++++ drop4/projectlistwidget.h | 32 +++++ 8 files changed, 504 insertions(+) create mode 100644 drop4/dialog.cpp create mode 100644 drop4/dialog.h create mode 100644 drop4/dialog.ui create mode 100644 drop4/drop4.pro create mode 100644 drop4/drop4.pro.user create mode 100644 drop4/main.cpp create mode 100644 drop4/projectlistwidget.cpp create mode 100644 drop4/projectlistwidget.h diff --git a/drop4/dialog.cpp b/drop4/dialog.cpp new file mode 100644 index 0000000..4e040ab --- /dev/null +++ b/drop4/dialog.cpp @@ -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; +} diff --git a/drop4/dialog.h b/drop4/dialog.h new file mode 100644 index 0000000..65b0685 --- /dev/null +++ b/drop4/dialog.h @@ -0,0 +1,31 @@ +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include +#include +#include +#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 diff --git a/drop4/dialog.ui b/drop4/dialog.ui new file mode 100644 index 0000000..f709d56 --- /dev/null +++ b/drop4/dialog.ui @@ -0,0 +1,20 @@ + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + + diff --git a/drop4/drop4.pro b/drop4/drop4.pro new file mode 100644 index 0000000..e4bcb77 --- /dev/null +++ b/drop4/drop4.pro @@ -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 diff --git a/drop4/drop4.pro.user b/drop4/drop4.pro.user new file mode 100644 index 0000000..0de9c25 --- /dev/null +++ b/drop4/drop4.pro.user @@ -0,0 +1,255 @@ + + + + + + EnvironmentId + {a8c18b7e-8020-49b9-a0ef-0165c784344f} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.3 MinGW 32bit + Desktop Qt 5.3 MinGW 32bit + qt.53.win32_mingw482_kit + 0 + 0 + 0 + + C:/Users/fzyz_sb/Desktop/build-drop4-Desktop_Qt_5_3_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + C:/Users/fzyz_sb/Desktop/build-drop4-Desktop_Qt_5_3_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 2 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + 在本地部署 + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + drop4 + + Qt4ProjectManager.Qt4RunConfiguration:C:/Users/fzyz_sb/Desktop/drop4/drop4.pro + + drop4.pro + false + false + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 16 + + + Version + 16 + + diff --git a/drop4/main.cpp b/drop4/main.cpp new file mode 100644 index 0000000..c6efa74 --- /dev/null +++ b/drop4/main.cpp @@ -0,0 +1,11 @@ +#include "dialog.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Dialog w; + w.show(); + + return a.exec(); +} diff --git a/drop4/projectlistwidget.cpp b/drop4/projectlistwidget.cpp new file mode 100644 index 0000000..6a1f50a --- /dev/null +++ b/drop4/projectlistwidget.cpp @@ -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(event->source()); + if (source && source != this) { + //确认是一个移动的操作 + event->setDropAction(Qt::MoveAction); + event->accept(); + } +} + +void ProjectListWidget::dragMoveEvent(QDragMoveEvent *event) +{ + ProjectListWidget *source = + qobject_cast(event->source()); + if (source && source != this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } +} + +void ProjectListWidget::dropEvent(QDropEvent *event) +{ + ProjectListWidget *source = + qobject_cast(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; + } +} diff --git a/drop4/projectlistwidget.h b/drop4/projectlistwidget.h new file mode 100644 index 0000000..c8f9939 --- /dev/null +++ b/drop4/projectlistwidget.h @@ -0,0 +1,32 @@ +#ifndef PROJECTLISTWIDGET_H +#define PROJECTLISTWIDGET_H + +#include +#include +#include +#include +#include +#include +#include +#include + +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