Qt将qgraphicsrectitem从一个Graphicsview拖放到另一个Graphicsview中

时间:2017-05-19 20:31:33

标签: drag-and-drop

我想将一个自定义QGraphicsRectItem从一个Graphicsview拖放到另一个Graphicsview中。我尝试使用QGraphicsSceneDragDropEvents和QDragEnterEvent,QDropEvent等。

(我知道我不需要所有这些包括)。

Layout.h

#ifndef LAYOUT_H
#define LAYOUT_H

#include <QtWidgets/QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QtWidgets>
#include <QObject>
#include <QCursor>
#include <QDrag>
#include "ui_layout.h"
#include "GrRectItm_WorkRect.h"
#include <QGraphicsSceneDragDropEvent>
#include <QBrush>
#include <QGraphicsSceneDragDropEvent>

class Layout : public QObject, public QMainWindow
{
Q_OBJECT

public:
Layout(QWidget *parent = 0);

~Layout();

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

/*void dragEnterEvent(QGraphicsSceneDragDropEvent *event) ;
void dragMoveEvent(QGraphicsSceneDragDropEvent *event) ;
void dragLeaveEvent(QGraphicsSceneDragDropEvent *event) ;
void dropEvent(QGraphicsSceneDragDropEvent *event) ;*/

void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
//void mousePressEvent(QMouseEvent *event);

private:
Ui::LayoutClass ui;

QGraphicsScene *tb;
QGraphicsScene *wa;

int mX;
int mY;

QPoint mousePosition;

QGraphicsRectItem *rect;

QBrush blueBrush = (Qt::blue);
QBrush redBrush = (Qt::red);
QBrush greenBrush = (Qt::green);
QPen blackPen = (Qt::black);

QBrush rectColor ;

QString str_text;

bool selected;
void newRect();

signals:    

public slots:   
void changeColor();
void changeText();
void deleteRect();
};

#endif // LAYOUT_H

Layout.cpp

#include "layout.h"
#include <QtWidgets>
#include <QGraphicsItem>
#include"GrRectItm_WorkRect.h"
#include <QGraphicsView>

GrRectItm_WorkRect *rectItem;
GrRectItm_WorkRect *location;
QList < GrRectItm_WorkRect *> items;

Layout::Layout(QWidget *parent)
: QMainWindow(parent)
{
qDebug() << "Programm startet";
ui.setupUi(this);

tb = new QGraphicsScene(this);
wa = new QGraphicsScene(this);  

ui.gv_ToolBar->setScene(tb);
ui.gv_WorkArea->setScene(wa);   

ui.gv_ToolBar->setAcceptDrops(true);
ui.gv_WorkArea->setAcceptDrops(true);
ui.gv_WorkArea->setDragMode(QGraphicsView::ScrollHandDrag);         

blackPen.setWidth(1);
rect = tb->addRect(10, 0, 90, 50, blackPen, blueBrush);
//rect->setFlag(QGraphicsItem::ItemIsSelectable);   

QObject::connect(ui.btn_Change, SIGNAL(clicked()), this, 
SLOT(changeColor()));

QObject::connect(ui.btn_Delete, SIGNAL(clicked()), this, SLOT(deleteRect()));   

}

//void Layout::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
void Layout::dragEnterEvent(QDragEnterEvent *event) {
qDebug() << "dragEnterEvent startet";
event->acceptProposedAction();      
}
//void Layout::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
void Layout::dragMoveEvent(QDragMoveEvent *event) { 
qDebug() << "dragMoveEvent startet";
event->acceptProposedAction();
}
void Layout::dropEvent(QDropEvent *event) {
//void Layout::dropEvent(QGraphicsSceneDragDropEvent *event) {
qDebug() << "dropEvent startet";

QRectF rect123(0, 0, 70, 40);

mX = mousePosition.x();
mY = mousePosition.y();

str_text = ui.le_Name->text();
rectItem = new GrRectItm_WorkRect(str_text);

//newRect();
wa->addItem(rectItem);
rectItem->setRect(rect123);
rectItem->setPos(mX, mY);
rectItem->setBrush(greenBrush);
items.append(rectItem); 
}


//void Layout::mousePressEvent(QGraphicsSceneMouseEvent *event) {
void Layout::mousePressEvent(QMouseEvent *event) {
qDebug() << "MousePressEvent startet";
mousePosition = event->pos();

mousePosition = mapToParent(event->pos() - mousePosition);

//if (rect->contains(mousePosition)) {
    QPixmap *pix = new QPixmap(100, 50);
    pix->fill(Qt::red);

    QDrag *drag = new QDrag(this);
    QMimeData *mime = new QMimeData;
    drag->setMimeData(mime);
    drag->setPixmap(*pix);
    Qt::DropAction dropAction = drag->exec();
//} 
}

void Layout::newRect() {
QString color = ui.cb_color->currentText();
QString text = ui.le_Name->text();

if (color == "Rot") {
    rectItem->setBrush(redBrush);
    rectItem->GrRectItm_WorkRect::setRectText(text);
}
else if (color == "Gruen") {
    rectItem->setBrush(greenBrush);
    rectItem->GrRectItm_WorkRect::setRectText(text);
}
else if (color == "Blau") {
    rectItem->setBrush(blueBrush);
    rectItem->GrRectItm_WorkRect::setRectText(text);
}
}
void Layout::changeColor() {
QString color = ui.cb_color->currentText();
QString text = ui.le_Name->text();
for (int i = 0; i < items.length(); i++) {

    location = items.at(i);
    selected = location->isSelected();

    if (selected) {

        rectItem = items.at(i);
        try
        {
            newRect();
        }
        catch (const std::exception&)
        {
            continue;
        }           
    }       
}
}
void Layout::deleteRect() { 
for (int i = 0; i < items.length(); i++) {
    rectItem = items.at(i);
    selected = rectItem->isSelected();
    if (selected) {
        try
        {
            rectItem->~GrRectItm_WorkRect();
            items.removeAt(i);
            i--;
        }
        catch (const std::exception&)
        {
            continue;
        }

    }
}
}
void Layout::changeText() {
QString text = ui.le_Name->text();
rectItem->GrRectItm_WorkRect::setRectText(text);
}
void Layout::mouseReleaseEvent(QMouseEvent *event) {
mousePosition = event->pos() - mousePosition;
}

Layout::~Layout()
{

}

0 个答案:

没有答案