Why QGraphicsProxyWidget does not resize properly after dropping into QGraphicsView?

后端 未结 1 1845
花落未央
花落未央 2021-01-27 05:43

I have a QListWidget and a QGraphicsView both subclassed to overwrite some of their members. I prepared a minimal verifiable example showing the proble

相关标签:
1条回答
  • 2021-01-27 06:30

    Cause

    You create proxyControl giving wgt->width() as a width:

    QGraphicsRectItem *proxyControl = addRect(initPos.x(), initPos.y(), wgt->width(), 20, QPen(Qt::black), QBrush(Qt::darkGreen));
    

    However, at that time wgt does not have its final geometry.

    Solution

    Setup proxyControl after you are done with wgt.

    Example

    Here is an example I have written for you to demonstrate how the proposed solution could be implemented. Change your dropEvent like this:

    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));
            }
        }
    }
    

    Note: Use QGraphicsSceneDragDropEvent::scenePos() to properly position the dropped item.

    Result

    The proposed change gives you the desired result right away:

    0 讨论(0)
提交回复
热议问题