在QGraphicsView中拖动QGraphicsWidget
生活随笔
收集整理的這篇文章主要介紹了
在QGraphicsView中拖动QGraphicsWidget
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如果你的QGraphicsWidget、QGraphicsItem無(wú)法相應(yīng)鼠標(biāo)、鍵盤消息,請(qǐng)參考《QGraphicsWidget收不到鼠標(biāo)、鍵盤消息解決》
兩種方法
方法1:
先通過(guò)下面的代碼設(shè)置被拖動(dòng)想的屬性:
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemUsesExtendedStyleOption);如果你的視圖是繼承自QGraphicsView,則在重載鼠標(biāo)移動(dòng)事件時(shí),基類QGraphicsView的鼠標(biāo)移動(dòng)事件請(qǐng)不要屏蔽,否則不能移動(dòng)項(xiàng),如下:
void GraphicsView::mouseMoveEvent(QMouseEvent *event) {// 這里根據(jù)業(yè)務(wù)寫你需要的代碼QGraphicsView::mouseMoveEvent(event); // 這句不能漏掉,否則項(xiàng)不能移動(dòng) }即上面的QGraphicsView::mouseMoveEvent(event);?這句不能漏掉,否則項(xiàng)不能移動(dòng)。
方法2:
重寫鼠標(biāo)事件
繼承QGraphicsItem,重寫mousePressEvent、mouseMoveEvent和mouseReleaseEvent三個(gè)函數(shù)。
void BreakerItem::mousePressEvent( QGraphicsSceneMouseEvent* event ) {m_mousePressed=true;m_mousePressedPoint=this->scenePos()-event->scenePos(); }void BreakerItem::mouseMoveEvent( QGraphicsSceneMouseEvent* event ) {if(m_mousePressed==true){this->setPos(event->scenePos()+m_mousePressedPoint);} } void BreakerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent* event ) {m_mousePressed=false; }方法2沒(méi)有親自測(cè)試過(guò),從網(wǎng)上找的。
總結(jié)
以上是生活随笔為你收集整理的在QGraphicsView中拖动QGraphicsWidget的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 理想汽车单车均价33万元!李想谈降价:车
- 下一篇: endl、flush、ends、unit