2015-09-22 35 views
0

我想以某種方式畫一個矩形選擇場景,顯示所選的項目(不是他項邊框,而邊框映射到場景 - 如果多選,選擇邊界矩形)。QGraphicsRectItem成員給出錯誤

我想嘗試類似的方式,在鼠標按下時顯示矩形(並根據當前選擇進行更新),並在鼠標釋放時隱藏它。

我無法保持矩形現場,並在釋放鼠標可以刪除它,也許這是從來沒有出現 - 我得到一個錯誤:

QGraphicsScene::removeItem: item 0x37828's scene (0x0) is different from this scene (0x1f57b68) 

(上述錯誤,以及鼠標按下後不停留的事實,使我認爲它沒有正確添加,但我不明白爲什麼)。

這裏是一個小示例代碼:

#include <QApplication> 
#include <QGraphicsView> 
#include <QGraphicsScene> 
#include <QGraphicsRectItem> 

class MyScene : public QGraphicsScene 
{ 
public: 
    MyScene(qreal x, qreal y, qreal w, qreal h) { 
     setSceneRect(x, y, w, h); 
     m_selectionRectangle = new QGraphicsRectItem(0,0,1,1); 
     m_selectionRectangle->setBrush(Qt::magenta); 
     m_selectionRectangle->setOpacity(0.2); 
    } 
    ~MyScene() { 
     if(m_selectionRectangle) 
      delete m_selectionRectangle; 
    } 
protected: 
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) { 
     QGraphicsScene::mousePressEvent(event); 
     if(!selectedItems().isEmpty()) { 
      QRectF selectionRect = QRectF(); 
      foreach(QGraphicsItem* item, selectedItems()) 
       selectionRect |= item->mapToScene(item->boundingRect()).boundingRect(); 
      m_selectionRectangle->setRect(selectionRect); 
      addItem(m_selectionRectangle); 
     } 
    } 
    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { 
     QGraphicsScene::mouseReleaseEvent(event); 
     removeItem(m_selectionRectangle); 
    } 
private: 
    QGraphicsRectItem* m_selectionRectangle; 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    MyScene* s = new MyScene(0, 0, 800, 600); 
    QGraphicsView view(s); 
    view.setDragMode(QGraphicsView::RubberBandDrag); 
    view.show(); 
    QGraphicsRectItem* xxx = new QGraphicsRectItem(200, 200, 100, 100); 
    QGraphicsEllipseItem* yyy = new QGraphicsEllipseItem(300, 300, 200, 100); 
    s->addItem(xxx); 
    s->addItem(yyy); 
    xxx->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsFocusable|QGraphicsItem::ItemIsSelectable); 
    yyy->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsFocusable|QGraphicsItem::ItemIsSelectable); 
    return app.exec(); 
} 

什麼是錯誤的意思,那我在加入選擇矩形做錯了,爲什麼不就呆在那裏 - 和我怎麼能修理它 ?

回答

0

錯誤的含義是字面意思:你正在將一個項目傳遞給removeItem,它不是你試圖從中刪除的場景的子項目。刪除不在場景中的項目是無稽之談。

當釋放鼠標按鈕時,沒有什麼能夠保證選擇矩形在場景中,因爲有通過mousePressEvent的路徑不會將矩形添加到場景中。我甚至不確定是否可以保證在每個發佈活動之前獲得新聞事件。

你如果是在現場,只除去矩形(且不需要virtual,但Q_DECL_OVERRIDE是!):

void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE { 
    QGraphicsScene::mouseReleaseEvent(event); 
    if (m_selectionRectangle.scene()) removeItem(m_selectionRectangle); 
} 

而且,你的自定義場景的析構函數是不必要的。只需按價值添加商品:

class MyScene : public QGraphicsScene 
{ 
    QGraphicsRectItem m_selectionRectangle; 
public: 
    MyScene(qreal x, qreal y, qreal w, qreal h) : 
     m_selectionRectangle(0, 0, 1 1) 
    { 
     setSceneRect(x, y, w, h); 
     m_selectionRectangle.setBrush(Qt::magenta); 
     m_selectionRectangle.setOpacity(0.2); 
    } 
protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE { 
     ... 
    } 
    ... 
};