如何获得放置在图形场景中的对象的掉落事件?

时间:2018-06-27 06:00:25

标签: qt drag-and-drop qwidget qgraphicsscene

我有图形视图(QGraphics视图)已设置场景(QGraphic场景)我正在将对象放置在其工作正常的场景上,我必须通过从参数列表中拖动参数来为放置的对象分配参数。我实现了对象的拖放事件。但是当我从对象上的参数列表非接受符号中拖动参数时,如何通过删除将参数分配给对象?欢迎任何其他建议和示例,使我可以将其付诸实践。

gui图片

speedometer.cpp

#include <QMimeData>   

SpeedoMeter::SpeedoMeter( QWidget *parent ):
    QwtDial( parent ),
    d_label( "km/h" )   
{

    setAcceptDrops(true);

}

void SpeedoMeter::dragEnterEvent(QDragEnterEvent *event)   
{    

    if (event->mimeData()->hasFormat(paramlistMimeType()))
    {
    qDebug()<<"dragenter event in speedo" ;
    event->accept();
    }

}

void SpeedoMeter::dragMoveEvent(QDragMoveEvent *event)
{

    if (event->mimeData()->hasFormat(paramlistMimeType()))
    {
    qDebug()<<"dragmove event in speedo" ; 
    event->acceptProposedAction();
    }

}

void SpeedoMeter::dropEvent(QDropEvent *event)
{

    if (event->mimeData()->hasFormat(paramlistMimeType()))
    {
     qDebug()<<"dragmove event in speedo" ;   
      event->accept();   
    }

}

1 个答案:

答案 0 :(得分:0)

以下示例显示了如何实现接受拖放的逻辑:

车速表.h

#ifndef SPEEDOMETER_H
#define SPEEDOMETER_H

#include <qwt_dial.h>

class SpeedoMeter : public QwtDial
{
public:
    SpeedoMeter(QWidget *parent=nullptr);

protected:
    void dragEnterEvent(QDragEnterEvent *event);
    void dropEvent(QDropEvent *event);
    void dragMoveEvent(QDragMoveEvent *event);
    void paintEvent(QPaintEvent *event);
private:
    QString d_label;
};


#endif // SPEEDOMETER_H

speedometer.cpp

#include "speedometer.h"

#include <qwt_dial_needle.h>

#include <QDragEnterEvent>
#include <QMimeData>
#include <QPainter>

SpeedoMeter::SpeedoMeter(QWidget *parent):
    QwtDial(parent),
    d_label( "km/h" )
{
    setAcceptDrops(true);
    QwtDialSimpleNeedle *nd = new QwtDialSimpleNeedle(QwtDialSimpleNeedle::Arrow, Qt::white, Qt::red);
    setNeedle(nd);
    setValue(80);
}

void SpeedoMeter::dragEnterEvent(QDragEnterEvent *event)
{
    if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
        event->acceptProposedAction();
}

void SpeedoMeter::dropEvent(QDropEvent *event)
{
    const QMimeData *mimedata = event->mimeData();

    if(mimedata->hasFormat("application/x-qabstractitemmodeldatalist")){
        QString text;
        // https://stackoverflow.com/questions/1723989/how-to-decode-application-x-qabstractitemmodeldatalist-in-qt-for-drag-and-drop
        QByteArray encoded = mimedata->data("application/x-qabstractitemmodeldatalist");
        QDataStream stream(&encoded, QIODevice::ReadOnly);
        while (!stream.atEnd()) {
            int row, col;
            QMap<int,  QVariant> roleDataMap;
            stream >> row >> col >> roleDataMap;
            if(roleDataMap.contains(Qt::DisplayRole)){
                text = roleDataMap[Qt::DisplayRole].toString();
                break;
            }
        }
        // your text
        d_label = text;
        update();
    }
}

void SpeedoMeter::dragMoveEvent(QDragMoveEvent *event)
{
    if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
        event->accept();
}

void SpeedoMeter::paintEvent(QPaintEvent *event)
{
    // https://stackoverflow.com/questions/43904204/qwt-dial-show-unit
    QwtDial::paintEvent(event);
    QPainter painter(this);
    painter.setPen(Qt::black);
    QFont font;
    font.setPointSize(11);
    painter.setFont(font);
    QString text = QString("%1 %2").arg(value()).arg(d_label);
    QPoint c = rect().center();
    QSize Size = painter.fontMetrics().size(Qt::TextSingleLine, text);
    painter.drawText(QPointF(c.x() -Size.width()/2, c.y() + 2.5*Size.height()), text);

}

main.cpp

#include "speedometer.h"

#include <QApplication>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QListWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QHBoxLayout *layout = new QHBoxLayout(&w);
    QListWidget listWidget;
    listWidget.setDragDropMode(QAbstractItemView::DragOnly);
    listWidget.addItems({"km/h", "ft/s", "m/s", "miles/h"});

    QGraphicsView view;
    QGraphicsScene scene;
    view.setScene(&scene);

    SpeedoMeter speed;
    scene.addWidget(&speed);
    layout->addWidget(&listWidget);
    layout->addWidget(&view);
    w.show();

    return a.exec();
}

enter image description here enter image description here

在下面的link中,您可以找到完整的示例。