Advertisement
Guest User

Untitled

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