2012-05-14 14 views
5

मैं QGraphicsRectItem subclassed है, और यह किसी भी माउस घटनाओं प्राप्त नहीं कर रहा है। मैंने इस तरह के अन्य प्रश्नों को देखा है कि मुझे माउस ट्रैकिंग सक्षम करने की आवश्यकता है, लेकिन setMouseTracking QWidget में है, और QGraphicsItem QWidget प्रतीत नहीं होता है।क्या मुझे QGraphicsItem में माउस ईवेंट मिल सकता है?

मैं paint को क्रियान्वित किया है, और है कि काम कर रहा है। मेरे subclassed QGraphicsView में मुझे माउस घटनाएं मिल रही हैं।

डॉक्स में सोचने के लिए मैं बस (उदाहरण के लिए) mousePressEvent समारोह ओवरराइड करना चाहिए लगते हैं और मैं घटनाओं में हो रही शुरू कर देना चाहिए। चाहे मैं माउस क्यूप्राइव को अपने QGraphicsView के सुपरक्लास पर अग्रेषित करता हूं या नहीं, ऐसा कोई फर्क नहीं पड़ता है।

उत्तर

11

अपने subclassed QGraphicsView में, आप ओवरराइड माउस घटना तरीकों के डिफ़ॉल्ट कार्यान्वयन कॉल करने के लिए यदि आप उन्हें आइटम करने के लिए नीचे का प्रचार करना चाहते हैं की जरूरत है। उदाहरण के लिए:

CustomView::mousePressEvent(QMouseEvent *event) 
{ 
    // handle the event as you like 

    QGraphicsView::mousePressEvent(event); // then call default implementation 
} 

आप मंडराना घटनाओं को स्वीकार करना चाहते हैं, तो आप QGraphicsItem::setAcceptHoverEvents(true); कॉल करने के लिए की जरूरत है। अन्यथा आपको किसी विशेष माउस ट्रैकिंग को सक्षम करने की आवश्यकता नहीं है।

संपादित करें:

#include <QtGui> 

class CustomView : public QGraphicsView 
{ 
protected: 
    void mousePressEvent(QMouseEvent *event) 
    { 
     qDebug() << "Custom view clicked."; 
     QGraphicsView::mousePressEvent(event); 
    } 
}; 

class CustomItem : public QGraphicsRectItem 
{ 
protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event) 
    { 
     qDebug() << "Custom item clicked."; 
    } 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    CustomItem item; 
    item.setRect(20, 20, 60, 60); 

    QGraphicsScene scene(0, 0, 100, 100); 
    scene.addItem(&item); 

    CustomView view; 
    view.setScene(&scene); 
    view.show(); 
    return a.exec(); 
} 
+0

मुझे लगता है कि, के रूप में सवाल में कहा गया है की कोशिश की। मैं QGraphicsItem में mousepress घटना पर एक ब्रेकपाइंट है, और इसे या नहीं, मैं ऐसा 'QGraphicsView :: mousePressEvent (घटना) की परवाह किए बिना कहा जाता नहीं है,' – Almo

+0

@Almo आप एक subclassed QGraphicsScene में किसी भी माउस घटनाओं अधिभावी रहे हैं? – Anthony

+0

मेरे क्यूग्राफिक्स व्यू उपclass में, मैं एक QGraphicsScene बनाते हैं, और इसके साथ सेटसेन कॉल करता हूं। मैंने QGraphicsScene subclassed नहीं है। – Almo

2

मैं एक ही समस्या है आप का सामना करना पड़ा माध्यम से चला गया और मैं एंथनी वास्तव में अच्छा जवाब के शीर्ष पर कुछ ऐसी जानकारियां दी जोड़ना चाहते थे: यहाँ एक पूर्ण काम कर उदाहरण है। यहां एक उदाहरण दिया गया है जिसमें मैंने कुछ विशेषताओं को दिखाया है जिन्हें माउस ईवेंट और कीबोर्ड ईवेंट का उपयोग करके कार्यान्वित किया जा सकता है।

ध्यान दें कि घटना QGraphicsItem एस QGraphicsItemGroup में या QList<QGraphicsItem> में प्रचारित नहीं होती है (मुझे इसे समझने में थोड़ी देर लग गई)।

#include <QtGui> 
#include <QGraphicsRectItem> 
#include <QGraphicsView> 
#include <QApplication> 
#include <QGraphicsSceneMouseEvent> 

class CustomItem : public QGraphicsEllipseItem 
{ 
protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event) 
    { 
     if(event->button() == Qt::LeftButton) { 
      if(event->modifiers() == Qt::ShiftModifier) { 
       qDebug() << "Custom item left clicked with shift key."; 
       // add the item to the selection 
       setSelected(true); 
      } else if(event->modifiers() == Qt::AltModifier){ 
       qDebug() << "Custom item left clicked with alt key."; 
       // resize the item 
       double radius = boundingRect().width()/2.0; 
       _center = QPointF(boundingRect().topLeft().x() + pos().x() + radius, boundingRect().topLeft().y() + pos().y() + radius); 
       QPointF pos = event->scenePos(); 
       qDebug() << boundingRect() << radius << this->pos() << pos << event->pos(); 
       double dist = sqrt(pow(_center.x()-pos.x(), 2) + pow(_center.y()-pos.y(), 2)); 
       if(dist/radius > 0.8) { 
        qDebug() << dist << radius << dist/radius; 
        _isResizing = true; 
       } else { 
        _isResizing = false; 
       } 
      } else { 
       qDebug() << "Custom item left clicked."; 
       QGraphicsItem::mousePressEvent(event); 
       event->accept(); 
      } 
     } else if(event->button() == Qt::RightButton) { 
      qDebug() << "Custom item right clicked."; 
      event->ignore(); 
     } 
    } 
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) 
    { 
     if(event->modifiers() == Qt::AltModifier && _isResizing){ 
      QPointF pos = event->scenePos(); 
      double dist = sqrt(pow(_center.x()-pos.x(), 2) + pow(_center.y()-pos.y(), 2)); 
      setRect(_center.x()-this->pos().x()-dist, _center.y()-this->pos().y()-dist, dist*2, dist*2); 
     } else if(event->modifiers() != Qt::AltModifier) { 
      qDebug() << "Custom item moved."; 
      QGraphicsItem::mouseMoveEvent(event); 
      qDebug()<<"moved"<<pos(); 
     } 
    } 
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 
    { 
     if(event->modifiers() == Qt::AltModifier && _isResizing) { 
      _isResizing = false; 
     } else if(event->modifiers() != Qt::ShiftModifier) { 
      QGraphicsItem::mouseReleaseEvent(event); 
     } 
    } 

    int type() const 
    { 
     // Enable the use of qgraphicsitem_cast with this item. 
     return UserType+1; 
    } 
private: 
    QPointF _center; 
    bool _isResizing; 
}; 

class CustomScene : public QGraphicsScene 
{ 
protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event) 
    { 
     qDebug() << "Custom scene clicked."; 
     QGraphicsScene::mousePressEvent(event); 
     if(!event->isAccepted()) { 
      if(event->button() == Qt::LeftButton) { 
       // add a custom item to the scene 
       QPointF pt = event->scenePos(); 
       CustomItem * item = new CustomItem(); 
       item->setRect(pt.x()-25, pt.y()-25, 50, 50); 
       item->setFlags(QGraphicsItem::ItemIsSelectable| 
           QGraphicsItem::ItemIsMovable); 
       addItem(item); 
      } else if(event->button() == Qt::RightButton) { 
       // check whether there is an item under the cursor 
       QGraphicsItem * itemToRemove = NULL; 
       foreach(auto item, items(event->scenePos())) { 
        if(item->type() == QGraphicsItem::UserType+1) { 
         itemToRemove = item; 
         break; 
        } 
       } 
       if(itemToRemove) { 
        // remove the item from the graphicsScene 
        removeItem(itemToRemove); 
       } 
      } 
     } 
    } 
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) 
    { 
     qDebug() << "Custom scene moved."; 
     QGraphicsScene::mouseMoveEvent(event); 
    } 
    void keyPressEvent(QKeyEvent * event) { 
     if(event->key() == Qt::Key_Backspace) { 
      // remove all selected items 
      qDebug() << "selected items" << selectedItems().size(); 
      while(!selectedItems().isEmpty()) { 
       removeItem(selectedItems().front()); 
      } 
     } else { 
      QGraphicsScene::keyPressEvent(event); 
     } 
    } 
}; 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    CustomItem item; 
    item.setRect(20, 20, 60, 60); 
    item.setFlags(QGraphicsItem::ItemIsSelectable| 
        QGraphicsItem::ItemIsMovable); 

    CustomScene scene; 
    scene.setSceneRect(0, 0, 500, 500); 
    scene.addItem(&item); 

    QGraphicsView view; 
    view.setScene(&scene); 
    view.show(); 
    return a.exec(); 
} 

उम्मीद है कि यह भी मदद करता है!

संबंधित मुद्दे