diff options
Diffstat (limited to 'examples/opengl/hellogl2')
-rw-r--r-- | examples/opengl/hellogl2/CMakeLists.txt | 38 | ||||
-rw-r--r-- | examples/opengl/hellogl2/mainwindow.cpp | 18 | ||||
-rw-r--r-- | examples/opengl/hellogl2/window.cpp | 80 | ||||
-rw-r--r-- | examples/opengl/hellogl2/window.h | 12 |
4 files changed, 83 insertions, 65 deletions
diff --git a/examples/opengl/hellogl2/CMakeLists.txt b/examples/opengl/hellogl2/CMakeLists.txt index f4b85a329a..74ef4a8c24 100644 --- a/examples/opengl/hellogl2/CMakeLists.txt +++ b/examples/opengl/hellogl2/CMakeLists.txt @@ -1,16 +1,13 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + cmake_minimum_required(VERSION 3.16) project(hellogl2 LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/hellogl2") - find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL OpenGLWidgets Widgets) +qt_standard_project_setup() + qt_add_executable(hellogl2 glwidget.cpp glwidget.h logo.cpp logo.h @@ -24,16 +21,23 @@ set_target_properties(hellogl2 PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(hellogl2 PUBLIC - Qt::Core - Qt::Gui - Qt::OpenGL - Qt::OpenGLWidgets - Qt::Widgets +target_link_libraries(hellogl2 PRIVATE + Qt6::Core + Qt6::Gui + Qt6::OpenGL + Qt6::OpenGLWidgets + Qt6::Widgets ) install(TARGETS hellogl2 - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET hellogl2 + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/opengl/hellogl2/mainwindow.cpp b/examples/opengl/hellogl2/mainwindow.cpp index 2416fd6057..5ee2b738c2 100644 --- a/examples/opengl/hellogl2/mainwindow.cpp +++ b/examples/opengl/hellogl2/mainwindow.cpp @@ -3,19 +3,19 @@ #include "mainwindow.h" #include "window.h" +#include <QApplication> +#include <QKeySequence> #include <QMenuBar> #include <QMenu> #include <QMessageBox> MainWindow::MainWindow() { - QMenuBar *menuBar = new QMenuBar; - QMenu *menuWindow = menuBar->addMenu(tr("&Window")); - QAction *addNew = new QAction(menuWindow); - addNew->setText(tr("Add new")); - menuWindow->addAction(addNew); - connect(addNew, &QAction::triggered, this, &MainWindow::onAddNew); - setMenuBar(menuBar); + QMenu *menuWindow = menuBar()->addMenu(tr("&Window")); + menuWindow->addAction(tr("Add new"), QKeySequence(Qt::CTRL | Qt::Key_N), + this, &MainWindow::onAddNew); + menuWindow->addAction(tr("Quit"), QKeySequence(Qt::CTRL | Qt::Key_Q), + qApp, QApplication::closeAllWindows); onAddNew(); } @@ -23,8 +23,8 @@ MainWindow::MainWindow() void MainWindow::onAddNew() { if (!centralWidget()) - setCentralWidget(new Window(this)); + setCentralWidget(new Window); else - QMessageBox::information(nullptr, tr("Cannot add new window"), + QMessageBox::information(this, tr("Cannot Add New Window"), tr("Already occupied. Undock first.")); } diff --git a/examples/opengl/hellogl2/window.cpp b/examples/opengl/hellogl2/window.cpp index 055dd5b5e3..5a7ddf826f 100644 --- a/examples/opengl/hellogl2/window.cpp +++ b/examples/opengl/hellogl2/window.cpp @@ -1,9 +1,8 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include "glwidget.h" #include "window.h" -#include "mainwindow.h" +#include "glwidget.h" #include <QSlider> #include <QVBoxLayout> #include <QHBoxLayout> @@ -11,9 +10,18 @@ #include <QPushButton> #include <QApplication> #include <QMessageBox> +#include <QMainWindow> -Window::Window(MainWindow *mw) - : mainWindow(mw) +static QMainWindow *findMainWindow() +{ + for (auto *w : QApplication::topLevelWidgets()) { + if (auto *mw = qobject_cast<QMainWindow *>(w)) + return mw; + } + return nullptr; +} + +Window::Window() { glWidget = new GLWidget; @@ -28,22 +36,19 @@ Window::Window(MainWindow *mw) connect(zSlider, &QSlider::valueChanged, glWidget, &GLWidget::setZRotation); connect(glWidget, &GLWidget::zRotationChanged, zSlider, &QSlider::setValue); - QVBoxLayout *mainLayout = new QVBoxLayout; - QHBoxLayout *container = new QHBoxLayout; + QVBoxLayout *mainLayout = new QVBoxLayout(this); + QWidget *w = new QWidget; + QHBoxLayout *container = new QHBoxLayout(w); container->addWidget(glWidget); container->addWidget(xSlider); container->addWidget(ySlider); container->addWidget(zSlider); - QWidget *w = new QWidget; - w->setLayout(container); mainLayout->addWidget(w); dockBtn = new QPushButton(tr("Undock"), this); connect(dockBtn, &QPushButton::clicked, this, &Window::dockUndock); mainLayout->addWidget(dockBtn); - setLayout(mainLayout); - xSlider->setValue(15 * 16); ySlider->setValue(345 * 16); zSlider->setValue(0 * 16); @@ -64,7 +69,7 @@ QSlider *Window::createSlider() void Window::keyPressEvent(QKeyEvent *e) { - if (e->key() == Qt::Key_Escape) + if (isWindow() && e->key() == Qt::Key_Escape) close(); else QWidget::keyPressEvent(e); @@ -72,26 +77,37 @@ void Window::keyPressEvent(QKeyEvent *e) void Window::dockUndock() { - if (parent()) { - setParent(nullptr); - setAttribute(Qt::WA_DeleteOnClose); - move(QGuiApplication::primaryScreen()->size().width() / 2 - width() / 2, - QGuiApplication::primaryScreen()->size().height() / 2 - height() / 2); - dockBtn->setText(tr("Dock")); - show(); - } else { - if (!mainWindow->centralWidget()) { - if (mainWindow->isVisible()) { - setAttribute(Qt::WA_DeleteOnClose, false); - dockBtn->setText(tr("Undock")); - mainWindow->setCentralWidget(this); - } else { - QMessageBox::information(nullptr, tr("Cannot dock"), - tr("Main window already closed")); - } - } else { - QMessageBox::information(nullptr, tr("Cannot dock"), - tr("Main window already occupied")); - } + if (parent()) + undock(); + else + dock(); +} + +void Window::dock() +{ + auto *mainWindow = findMainWindow(); + if (mainWindow == nullptr || !mainWindow->isVisible()) { + QMessageBox::information(this, tr("Cannot Dock"), + tr("Main window already closed")); + return; } + if (mainWindow->centralWidget()) { + QMessageBox::information(this, tr("Cannot Dock"), + tr("Main window already occupied")); + return; + } + setAttribute(Qt::WA_DeleteOnClose, false); + dockBtn->setText(tr("Undock")); + mainWindow->setCentralWidget(this); +} + +void Window::undock() +{ + setParent(nullptr); + setAttribute(Qt::WA_DeleteOnClose); + const auto geometry = screen()->availableGeometry(); + move(geometry.x() + (geometry.width() - width()) / 2, + geometry.y() + (geometry.height() - height()) / 2); + dockBtn->setText(tr("Dock")); + show(); } diff --git a/examples/opengl/hellogl2/window.h b/examples/opengl/hellogl2/window.h index f9e0607903..edbd643ae2 100644 --- a/examples/opengl/hellogl2/window.h +++ b/examples/opengl/hellogl2/window.h @@ -6,20 +6,17 @@ #include <QWidget> -QT_BEGIN_NAMESPACE -class QSlider; -class QPushButton; -QT_END_NAMESPACE +QT_FORWARD_DECLARE_CLASS(QSlider) +QT_FORWARD_DECLARE_CLASS(QPushButton) class GLWidget; -class MainWindow; class Window : public QWidget { Q_OBJECT public: - Window(MainWindow *mw); + Window(); protected: void keyPressEvent(QKeyEvent *event) override; @@ -28,6 +25,8 @@ private slots: void dockUndock(); private: + void dock(); + void undock(); QSlider *createSlider(); GLWidget *glWidget; @@ -35,7 +34,6 @@ private: QSlider *ySlider; QSlider *zSlider; QPushButton *dockBtn; - MainWindow *mainWindow; }; #endif |