2
0

example_widget_qt.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <vulkan/vulkan.hpp>
  2. #include <vkw/QtVulkanWidget.h>
  3. #include <QMainWindow>
  4. #include <QApplication>
  5. #include <QPlainTextEdit>
  6. #include <QVulkanInstance>
  7. #include <QLibraryInfo>
  8. #include <QLoggingCategory>
  9. #include <QPointer>
  10. #include <QHBoxLayout>
  11. #include <QPushButton>
  12. #include <iostream>
  13. Q_LOGGING_CATEGORY(lcVk, "qt.vulkan")
  14. static QPointer<QPlainTextEdit> messageLogWidget;
  15. static QtMessageHandler oldMessageHandler = nullptr;
  16. static void messageHandler(QtMsgType msgType, const QMessageLogContext &logContext, const QString &text)
  17. {
  18. if (!messageLogWidget.isNull())
  19. messageLogWidget->appendPlainText(text);
  20. if (oldMessageHandler)
  21. oldMessageHandler(msgType, logContext, text);
  22. }
  23. // This header file contains the actual
  24. // rendering code for vulkan. It can be used
  25. // by both the SDLWidget and the Qt widget.
  26. // see example_widget_sdl.cpp
  27. // and example_widget_qt.cpp
  28. #include "example_myApplication.h"
  29. // to be able to get input events, you need to
  30. // inherit from the vkw::QtVulkanWidget class
  31. class MyQtVulkanWindow : public vkw::QtVulkanWidget
  32. {
  33. void mousePressEvent(QMouseEvent * e) override
  34. {
  35. }
  36. void mouseReleaseEvent(QMouseEvent * e) override
  37. {
  38. }
  39. void mouseDoubleClickEvent(QMouseEvent * e) override
  40. {
  41. }
  42. void mouseMoveEvent(QMouseEvent * e) override
  43. {
  44. }
  45. void keyPressEvent(QKeyEvent * e) override
  46. {
  47. }
  48. void keyReleaseEvent(QKeyEvent * e) override
  49. {
  50. }
  51. void wheelEvent(QWheelEvent * e) override
  52. {
  53. }
  54. };
  55. int main(int argc, char *argv[])
  56. {
  57. QApplication app(argc, argv);
  58. messageLogWidget = new QPlainTextEdit(QLatin1String(QLibraryInfo::build()) + QLatin1Char('\n'));
  59. messageLogWidget->setReadOnly(true);
  60. oldMessageHandler = qInstallMessageHandler(messageHandler);
  61. QLoggingCategory::setFilterRules(QStringLiteral("qt.vulkan=true"));
  62. QVulkanInstance inst;
  63. inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation");
  64. if (!inst.create())
  65. qFatal("Failed to create Vulkan instance: %d", inst.errorCode());
  66. QMainWindow * m_window = new QMainWindow();
  67. //=============================================================
  68. // This the main VulkanWidget
  69. //=============================================================
  70. // create a QtVulkanWidget and
  71. // set the vulkan instance
  72. MyQtVulkanWindow * vulkanWindow = new MyQtVulkanWindow();
  73. vulkanWindow->setVulkanInstance(&inst);
  74. // create a wrapper for the vulkanWindow
  75. QWidget *wrapper = QWidget::createWindowContainer(vulkanWindow);
  76. // Create an instance of our application.
  77. // this is the actual app that will be
  78. // performing all the rendering
  79. MyApplication * myApp = new MyApplication();
  80. vulkanWindow->init( myApp );
  81. // create a very simple user interface;
  82. // +------------------------------------+
  83. // | |
  84. // +--------------------------+---------+
  85. // | | |
  86. // | | |
  87. // | | |
  88. // | | |
  89. // | vulkan widget | other |
  90. // | | |
  91. // | | |
  92. // | | |
  93. // | | |
  94. // +--------------------------+---------+
  95. auto l = new QHBoxLayout();
  96. auto centralWidget = new QWidget();
  97. centralWidget->setLayout(l);
  98. m_window->setCentralWidget(centralWidget);
  99. l->addWidget(wrapper, 5);
  100. l->addWidget(new QPushButton(m_window), 1);
  101. m_window->resize(1024,768);
  102. m_window->show();
  103. return app.exec();
  104. }