summaryrefslogtreecommitdiffstats
path: root/examples/opengl/qopenglwidget/mainwindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/opengl/qopenglwidget/mainwindow.cpp')
-rw-r--r--examples/opengl/qopenglwidget/mainwindow.cpp56
1 files changed, 52 insertions, 4 deletions
diff --git a/examples/opengl/qopenglwidget/mainwindow.cpp b/examples/opengl/qopenglwidget/mainwindow.cpp
index eb34f87cb7..b3cbb4a69b 100644
--- a/examples/opengl/qopenglwidget/mainwindow.cpp
+++ b/examples/opengl/qopenglwidget/mainwindow.cpp
@@ -59,13 +59,16 @@
#include <QRandomGenerator>
#include <QSpinBox>
#include <QScrollArea>
+#include <QTabWidget>
+#include <QTabBar>
+#include <QToolButton>
#include "glwidget.h"
MainWindow::MainWindow()
: m_nextX(1), m_nextY(1)
{
- GLWidget *glwidget = new GLWidget(this, true, qRgb(20, 20, 50));
+ GLWidget *glwidget = new GLWidget(this, qRgb(20, 20, 50));
m_glWidgets << glwidget;
QLabel *label = new QLabel(this);
m_timer = new QTimer(this);
@@ -127,6 +130,7 @@ MainWindow::MainWindow()
QAction *showBubbles = showMenu->addAction("Show bubbles", glwidget, &GLWidget::setShowBubbles);
showBubbles->setCheckable(true);
showBubbles->setChecked(true);
+ showMenu->addAction("Open tab window", this, &MainWindow::showNewWindow);
QMenu *helpMenu = menuBar()->addMenu("&Help");
helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
@@ -156,9 +160,9 @@ void MainWindow::addNew()
{
if (m_nextY == 4)
return;
- GLWidget *w = new GLWidget(this, false, qRgb(QRandomGenerator::global()->bounded(256),
- QRandomGenerator::global()->bounded(256),
- QRandomGenerator::global()->bounded(256)));
+ GLWidget *w = new GLWidget(nullptr, qRgb(QRandomGenerator::global()->bounded(256),
+ QRandomGenerator::global()->bounded(256),
+ QRandomGenerator::global()->bounded(256)));
m_glWidgets << w;
connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update));
m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
@@ -185,3 +189,47 @@ void MainWindow::resizeEvent(QResizeEvent *)
{
m_glWidgets[0]->setMinimumSize(size() + QSize(128, 128));
}
+
+void MainWindow::showNewWindow()
+{
+ QTabWidget *tabs = new QTabWidget;
+ tabs->resize(800, 600);
+
+ QToolButton *tb = new QToolButton;
+ tb->setText(QLatin1String("+"));
+ tabs->addTab(new QLabel(QLatin1String("Add OpenGL widgets with +")), QString());
+ tabs->setTabEnabled(0, false);
+ tabs->tabBar()->setTabButton(0, QTabBar::RightSide, tb);
+ tabs->tabBar()->setTabsClosable(true);
+ QObject::connect(tabs->tabBar(), &QTabBar::tabCloseRequested, tabs, [tabs](int index) {
+ tabs->widget(index)->deleteLater();
+ });
+
+ const QString msgToTopLevel = QLatin1String("Break out to top-level window");
+ const QString msgFromTopLevel = QLatin1String("Move back under tab widget");
+
+ QObject::connect(tb, &QAbstractButton::clicked, tabs, [=] {
+ GLWidget *glwidget = new GLWidget(nullptr, Qt::blue);
+ glwidget->resize(tabs->size());
+ glwidget->setWindowTitle(QString::asprintf("QOpenGLWidget %p", glwidget));
+
+ QPushButton *btn = new QPushButton(msgToTopLevel, glwidget);
+ connect(btn, &QPushButton::clicked, glwidget, [=] {
+ if (glwidget->parent()) {
+ glwidget->setAttribute(Qt::WA_DeleteOnClose, true);
+ glwidget->setParent(nullptr);
+ glwidget->show();
+ btn->setText(msgFromTopLevel);
+ } else {
+ glwidget->setAttribute(Qt::WA_DeleteOnClose, false);
+ tabs->addTab(glwidget, glwidget->windowTitle());
+ btn->setText(msgToTopLevel);
+ }
+ });
+
+ tabs->setCurrentIndex(tabs->addTab(glwidget, glwidget->windowTitle()));
+ });
+
+ tabs->setAttribute(Qt::WA_DeleteOnClose);
+ tabs->show();
+}