Advertisement
Guest User

QQuickItem mouse events

a guest
Aug 16th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. main.cpp:
  2. int main(int argc, char *argv[])
  3. {
  4. using namespace vl;
  5.  
  6. qmlRegisterType<QQuickViewport>("VP", 0, 1, "Viewport");
  7. QGuiApplication app(argc, argv, 5);
  8. QQuickView * view = new QQuickView();
  9.  
  10. view->setResizeMode(QQuickView::SizeRootObjectToView);
  11. view->resize(600, 400);
  12. view->setSource(QUrl("main.qml"));
  13. view->show();
  14. return app.exec();
  15. }
  16.  
  17. QQuickViewport.h
  18. class QQuickViewport : public QQuickItem
  19. {
  20. Q_OBJECT
  21. public:
  22. QQuickViewport(QQuickItem * parent = 0);
  23. protected:
  24. QSGNode *updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData * data);
  25. };
  26.  
  27. class QSGFrameBufferTextureNode : public QObject, public QSGSimpleTextureNode
  28. {
  29. Q_OBJECT
  30. public:
  31. QSGFrameBufferTextureNode(QQuickWindow *window);
  32. ~QSGFrameBufferTextureNode();
  33. virtual void preprocess();
  34. protected:
  35. virtual void mousePressEvent(QMouseEvent * event);
  36. virtual void hoverEnterEvent(QHoverEvent * event);
  37. private:
  38. std::unique_ptr<QOpenGLFramebufferObject> frame_buffer_object_;
  39. std::unique_ptr<QSGTexture> texture_;
  40. QQuickWindow *window_;
  41. };
  42.  
  43. QQuickViewport.cpp
  44. QQuickViewport::QQuickViewport( QQuickItem * parent /*= 0*/ ) :
  45. QQuickItem(parent)
  46. {
  47. setFlags(ItemHasContents | ItemIsFocusScope);
  48. setAcceptedMouseButtons(Qt::LeftButton | Qt::MiddleButton | Qt::RightButton);
  49. setAcceptHoverEvents(true);
  50. }
  51.  
  52.  
  53. QSGNode * QQuickViewport::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
  54. {
  55. QSGFrameBufferTextureNode *node = static_cast<QSGFrameBufferTextureNode *>(oldNode);
  56. if (!node)
  57. node = new QSGFrameBufferTextureNode(window());
  58.  
  59. node->setRect(boundingRect());
  60. node->setFlags(QSGNode::UsePreprocess);
  61. return node;
  62. }
  63.  
  64. QSGFrameBufferTextureNode::QSGFrameBufferTextureNode( QQuickWindow *window ) :
  65. window_(window)
  66. {
  67. }
  68.  
  69. QSGFrameBufferTextureNode::~QSGFrameBufferTextureNode()
  70. {
  71. }
  72.  
  73. void QSGFrameBufferTextureNode::preprocess()
  74. {
  75. QSize size = rect().size().toSize();
  76.  
  77. QOpenGLFramebufferObjectFormat format;
  78. format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
  79.  
  80. if (!frame_buffer_object_ || frame_buffer_object_->size() != size)
  81. {
  82. frame_buffer_object_.reset(new QOpenGLFramebufferObject(size, format));
  83. texture_.reset(window_->createTextureFromId(frame_buffer_object_->texture(), size));
  84. setTexture(texture_.get());
  85. }
  86.  
  87. frame_buffer_object_->bind();
  88.  
  89. glViewport(0, 0, size.width(), size.height());
  90.  
  91.  
  92. LogoRenderer renderer;
  93. renderer.initialize();
  94. renderer.render();
  95.  
  96. frame_buffer_object_->bindDefault();
  97.  
  98. window_->update();
  99. }
  100.  
  101. void QSGFrameBufferTextureNode::mousePressEvent( QMouseEvent * event )
  102. {
  103. qDebug() << event->isAccepted();
  104. }
  105.  
  106. void QSGFrameBufferTextureNode::hoverEnterEvent( QHoverEvent * event )
  107. {
  108. qDebug() << event->isAccepted();
  109. }
  110.  
  111. script:
  112. import QtQuick 2.1
  113. import VP 0.1
  114.  
  115. Item
  116. {
  117. id: item
  118. width: 400
  119. height: 300
  120.  
  121. Viewport
  122. {
  123. id: renderer
  124. anchors.fill : parent
  125. visible: true
  126. enabled: true
  127. opacity: 1.0
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement