如何正确地将小部件放在鼠标下方的 QGraphicsView 上?

How to properly drop a widget on a QGraphicsView right under the mouse?

我一直在尝试用 QGraphicsView.

解决定位问题

将小部件从 QListWidget 拖放到 QGraphicsView 中后,它会到处跳转。

问题 我一直试图在另一位用户的帮助下解决以下问题: 一旦将小部件拖入 QGraphicsView 并放下。它只是在随机位置,必须注意取回它。此行为对用户不友好,有时需要一点时间才能找到小部件。

代码下方:

scene.h

#ifndef SCENE_H
#define SCENE_H

#include <QGraphicsScene>

class Scene : public QGraphicsScene
{
public:
    Scene(QObject *parent = nullptr);

protected:
  void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
  void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
  void dropEvent(QGraphicsSceneDragDropEvent *event);
};

#endif // SCENE_H

scene.cpp

void Scene::dropEvent(QGraphicsSceneDragDropEvent *event)
{
    QByteArray encoded =
            event->mimeData()->data("application/x-qabstractitemmodeldatalist");
    QDataStream stream(&encoded, QIODevice::ReadOnly);

    QStringList rosTables;
    QString newString;

    while (!stream.atEnd()) {
        int row, col;
        QMap<int, QVariant> roleDataMap;
        stream >> row >> col >> roleDataMap;
        rosTables << roleDataMap[Qt::DisplayRole].toString();
    }

    for (const QString &tableType : rosTables) {
        if (tableType == "Images") {
            QPoint initPos(0, 0);
            auto *wgt = new CustomTableWidget;
            auto *proxyControl = addRect(0, 0, 0, 0, QPen(Qt::black),
                                         QBrush(Qt::darkGreen));
            auto *sizeGrip = new QSizeGrip(wgt);
            auto *layout = new QHBoxLayout(wgt);

            layout->setContentsMargins(0, 0, 0, 0);
            layout->addWidget(sizeGrip, 0, Qt::AlignRight | Qt::AlignBottom);

            connect(wgt, &CustomTableWidget::sizeChanged, [wgt, proxyControl](){
                proxyControl->setRect(wgt->geometry().adjusted(-10, -10, 10, 10));
            });

            wgt->setColumnCount(2);
            wgt->setRowCount(2);

            for (int ridx = 0; ridx < wgt->rowCount(); ridx++) {
                for (int cidx = 0; cidx < wgt->columnCount(); cidx++) {
                    auto *item = new QTableWidgetItem();

                    item->setText(QString("%1").arg(ridx));
                    wgt->setItem(ridx,cidx,item);
                }
            }

            auto *const proxy = addWidget(wgt);


            proxy->setPos(initPos.x(), initPos.y()
                          + proxyControl->rect().height());
            proxy->setParentItem(proxyControl);

            proxyControl->setPos(initPos.x(), initPos.y());
            proxyControl->setFlag(QGraphicsItem::ItemIsMovable, true);
            proxyControl->setFlag(QGraphicsItem::ItemIsSelectable, true);
            proxyControl->setRect(wgt->geometry().adjusted(-10, -10, 10, 10));
        }
    }
}

目前尝试解决的问题:

现在 QGraphicsScene 已被子类化以重写鼠标事件,特别是在这种情况下,注意力集中在 dropEvent(QGraphicsSceneDragDropEvent *event) 因为这是负责“查看小部件”的函数“

进行了多次试验和错误,并在同一功能中尝试添加以下部分:

for (const QString &tableType : rosTables) {
    if (tableType == "Images") {
        QPoint initPos(event->scenePos()); // <-- Tried this but no change 
        auto *wgt = new CustomTableWidget;
        auto *proxyControl = addRect(0, 0, 0, 0, QPen(Qt::black),
                                     QBrush(Qt::darkGreen));
        auto *sizeGrip = new QSizeGrip(wgt);
        auto *layout = new QHBoxLayout(wgt);
}

尝试的另一件事是提供 QPoint 被认为适合目标的 event->scenePos().toPoint(),但不幸的是,这也没有解决问题:

for (const QString &tableType : rosTables) {
    if (tableType == "Images") {
        QPoint initPos(event->scenePos().toPoint()); // <-- Tried this too but no change
        auto *wgt = new CustomTableWidget;
        auto *proxyControl = addRect(0, 0, 0, 0, QPen(Qt::black),
                                     QBrush(Qt::darkGreen));
        auto *sizeGrip = new QSizeGrip(wgt);
        auto *layout = new QHBoxLayout(wgt);
} 

如果有人知道问题出在哪里,请提供解决方法的指导。

  1. proxy->setPos(initPos.x(), initPos.y() + proxyControl->rect().height()); 更改为 proxy->setPos(10, 10);

  2. 设置场景矩形:

     Scene::Scene(QObject *parent) :
         QGraphicsScene(parent)
     {
         setBackgroundBrush(Qt::lightGray);
         setSceneRect(0, 0, 1000, 1000);
     }
    
  3. 设置视图对齐方式,例如在 MainWindow.cpp:

     ui->graphicsView->setAlignment(Qt::AlignLeft | Qt::AlignTop);