ctkWidgetsUtilsTestGrabWidget.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // QT includes
  15. #include <QApplication>
  16. #include <QDialog>
  17. #include <QFrame>
  18. #include <QGLWidget>
  19. #include <QTimer>
  20. #include <QVBoxLayout>
  21. // CTK includes
  22. #include "ctkWidgetsUtils.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. int ctkWidgetsUtilsTestGrabWidget(int argc, char * argv [] )
  28. {
  29. QApplication app(argc, argv);
  30. QFrame parentWidget;
  31. parentWidget.setFrameStyle(QFrame::Panel | QFrame::Raised);
  32. parentWidget.setLineWidth(2);
  33. QGLWidget glWidget(&parentWidget);
  34. QVBoxLayout* layout = new QVBoxLayout(&parentWidget);
  35. layout->addWidget(&glWidget);
  36. parentWidget.setLayout(layout);
  37. parentWidget.resize(200, 200);
  38. parentWidget.show();
  39. // Add a dialog to cover vtkWidget to test if grabWidget works even when the
  40. // opengl view is covered.
  41. QDialog dialog(0);
  42. dialog.move(parentWidget.pos());
  43. dialog.show();
  44. QImage screenshot =
  45. ctk::grabWidget(&parentWidget);
  46. if (QColor(screenshot.pixel(100, 100)) != QColor(Qt::black))
  47. {
  48. std::cout << "Failed to grab QGLWidget, pixel at (100,100)="
  49. << screenshot.pixel(100, 100) << " " << QColor(Qt::black).rgb() << std::endl;
  50. return EXIT_FAILURE;
  51. }
  52. if (argc < 2 || QString(argv[1]) != "-I")
  53. {
  54. QTimer::singleShot(100, &app, SLOT(quit()));
  55. }
  56. return app.exec();
  57. }