hellogl2 example: Streamline code
- Shorten menu code, add keyboard shortcuts and a quit action. - Avoid calls to setLayout(). - Pass a parent to message boxes. - Use the current screen for size calculation and consider availableGeometry(). - Prevent closing by the Escape key in docked mode. - Split the dockUndock() function into dock()/undock() for clarity Change-Id: I007da3bff86ee3f2dc8f87379e5d2ba2f0f6f3d7 Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io> (cherry picked from commit 6f8398c8eb5476a6e98126d5b63421896b874271) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 734db610c3db3f00775d518b63d94695a2654389)
This commit is contained in:
parent
de59f732fc
commit
ccfd8acd4b
@ -3,19 +3,19 @@
|
|||||||
|
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QKeySequence>
|
||||||
#include <QMenuBar>
|
#include <QMenuBar>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
|
||||||
MainWindow::MainWindow()
|
MainWindow::MainWindow()
|
||||||
{
|
{
|
||||||
QMenuBar *menuBar = new QMenuBar;
|
QMenu *menuWindow = menuBar()->addMenu(tr("&Window"));
|
||||||
QMenu *menuWindow = menuBar->addMenu(tr("&Window"));
|
menuWindow->addAction(tr("Add new"), QKeySequence(Qt::CTRL | Qt::Key_N),
|
||||||
QAction *addNew = new QAction(menuWindow);
|
this, &MainWindow::onAddNew);
|
||||||
addNew->setText(tr("Add new"));
|
menuWindow->addAction(tr("Quit"), QKeySequence(Qt::CTRL | Qt::Key_Q),
|
||||||
menuWindow->addAction(addNew);
|
qApp, QApplication::closeAllWindows);
|
||||||
connect(addNew, &QAction::triggered, this, &MainWindow::onAddNew);
|
|
||||||
setMenuBar(menuBar);
|
|
||||||
|
|
||||||
onAddNew();
|
onAddNew();
|
||||||
}
|
}
|
||||||
@ -25,6 +25,6 @@ void MainWindow::onAddNew()
|
|||||||
if (!centralWidget())
|
if (!centralWidget())
|
||||||
setCentralWidget(new Window(this));
|
setCentralWidget(new Window(this));
|
||||||
else
|
else
|
||||||
QMessageBox::information(nullptr, tr("Cannot add new window"),
|
QMessageBox::information(this, tr("Cannot Add New Window"),
|
||||||
tr("Already occupied. Undock first."));
|
tr("Already occupied. Undock first."));
|
||||||
}
|
}
|
||||||
|
@ -28,22 +28,19 @@ Window::Window(MainWindow *mw)
|
|||||||
connect(zSlider, &QSlider::valueChanged, glWidget, &GLWidget::setZRotation);
|
connect(zSlider, &QSlider::valueChanged, glWidget, &GLWidget::setZRotation);
|
||||||
connect(glWidget, &GLWidget::zRotationChanged, zSlider, &QSlider::setValue);
|
connect(glWidget, &GLWidget::zRotationChanged, zSlider, &QSlider::setValue);
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||||||
QHBoxLayout *container = new QHBoxLayout;
|
QWidget *w = new QWidget;
|
||||||
|
QHBoxLayout *container = new QHBoxLayout(w);
|
||||||
container->addWidget(glWidget);
|
container->addWidget(glWidget);
|
||||||
container->addWidget(xSlider);
|
container->addWidget(xSlider);
|
||||||
container->addWidget(ySlider);
|
container->addWidget(ySlider);
|
||||||
container->addWidget(zSlider);
|
container->addWidget(zSlider);
|
||||||
|
|
||||||
QWidget *w = new QWidget;
|
|
||||||
w->setLayout(container);
|
|
||||||
mainLayout->addWidget(w);
|
mainLayout->addWidget(w);
|
||||||
dockBtn = new QPushButton(tr("Undock"), this);
|
dockBtn = new QPushButton(tr("Undock"), this);
|
||||||
connect(dockBtn, &QPushButton::clicked, this, &Window::dockUndock);
|
connect(dockBtn, &QPushButton::clicked, this, &Window::dockUndock);
|
||||||
mainLayout->addWidget(dockBtn);
|
mainLayout->addWidget(dockBtn);
|
||||||
|
|
||||||
setLayout(mainLayout);
|
|
||||||
|
|
||||||
xSlider->setValue(15 * 16);
|
xSlider->setValue(15 * 16);
|
||||||
ySlider->setValue(345 * 16);
|
ySlider->setValue(345 * 16);
|
||||||
zSlider->setValue(0 * 16);
|
zSlider->setValue(0 * 16);
|
||||||
@ -64,7 +61,7 @@ QSlider *Window::createSlider()
|
|||||||
|
|
||||||
void Window::keyPressEvent(QKeyEvent *e)
|
void Window::keyPressEvent(QKeyEvent *e)
|
||||||
{
|
{
|
||||||
if (e->key() == Qt::Key_Escape)
|
if (isWindow() && e->key() == Qt::Key_Escape)
|
||||||
close();
|
close();
|
||||||
else
|
else
|
||||||
QWidget::keyPressEvent(e);
|
QWidget::keyPressEvent(e);
|
||||||
@ -72,26 +69,36 @@ void Window::keyPressEvent(QKeyEvent *e)
|
|||||||
|
|
||||||
void Window::dockUndock()
|
void Window::dockUndock()
|
||||||
{
|
{
|
||||||
if (parent()) {
|
if (parent())
|
||||||
setParent(nullptr);
|
undock();
|
||||||
setAttribute(Qt::WA_DeleteOnClose);
|
else
|
||||||
move(QGuiApplication::primaryScreen()->size().width() / 2 - width() / 2,
|
dock();
|
||||||
QGuiApplication::primaryScreen()->size().height() / 2 - height() / 2);
|
}
|
||||||
dockBtn->setText(tr("Dock"));
|
|
||||||
show();
|
void Window::dock()
|
||||||
} else {
|
{
|
||||||
if (!mainWindow->centralWidget()) {
|
if (!mainWindow->isVisible()) {
|
||||||
if (mainWindow->isVisible()) {
|
QMessageBox::information(this, tr("Cannot Dock"),
|
||||||
setAttribute(Qt::WA_DeleteOnClose, false);
|
tr("Main window already closed"));
|
||||||
dockBtn->setText(tr("Undock"));
|
return;
|
||||||
mainWindow->setCentralWidget(this);
|
}
|
||||||
} else {
|
if (mainWindow->centralWidget()) {
|
||||||
QMessageBox::information(nullptr, tr("Cannot dock"),
|
QMessageBox::information(this, tr("Cannot Dock"),
|
||||||
tr("Main window already closed"));
|
tr("Main window already occupied"));
|
||||||
}
|
return;
|
||||||
} else {
|
}
|
||||||
QMessageBox::information(nullptr, tr("Cannot dock"),
|
setAttribute(Qt::WA_DeleteOnClose, false);
|
||||||
tr("Main window already occupied"));
|
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();
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,8 @@ private slots:
|
|||||||
void dockUndock();
|
void dockUndock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void dock();
|
||||||
|
void undock();
|
||||||
QSlider *createSlider();
|
QSlider *createSlider();
|
||||||
|
|
||||||
GLWidget *glWidget;
|
GLWidget *glWidget;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user