-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
74 lines (61 loc) · 2.19 KB
/
mainwindow.cpp
File metadata and controls
74 lines (61 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "mainwindow.h"
#include <QMenuBar>
#include <QApplication>
#include <QDockWidget>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("QtDock");
setDockOptions(AnimatedDocks | AllowNestedDocks | AllowTabbedDocks);
setDockNestingEnabled(true);
// No central widget — dock widgets fill the entire window
takeCentralWidget();
setMinimumSize(200, 150);
QMenu *fileMenu = menuBar()->addMenu("&File");
fileMenu->addAction("&New", QKeySequence::New, this, &MainWindow::newDocument);
fileMenu->addAction("E&xit", QKeySequence::Quit, qApp, &QApplication::quit);
resize(1024, 768);
}
void MainWindow::newDocument()
{
m_docCount++;
auto *dock = new QDockWidget(tr("Document %1").arg(m_docCount), this);
dock->setFeatures(QDockWidget::DockWidgetClosable |
QDockWidget::DockWidgetMovable |
QDockWidget::DockWidgetFloatable);
auto *editor = new QTextEdit(dock);
editor->setPlaceholderText(tr("Document %1").arg(m_docCount));
editor->setMinimumSize(50, 50);
dock->setWidget(editor);
// Use native window frame when floating so dragging works on Wayland/Linux
connect(dock, &QDockWidget::topLevelChanged, this, [dock](bool floating) {
if (floating) {
dock->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
dock->show();
}
});
addDockWidget(Qt::TopDockWidgetArea, dock);
// Find the dock widget that currently has focus
QDockWidget *target = nullptr;
QWidget *focused = QApplication::focusWidget();
while (focused) {
if (auto *dw = qobject_cast<QDockWidget *>(focused)) {
target = dw;
break;
}
focused = focused->parentWidget();
}
// Fall back to the first non-floating dock widget
if (!target) {
for (QDockWidget *existing : findChildren<QDockWidget *>()) {
if (existing != dock && !existing->isFloating()) {
target = existing;
break;
}
}
}
if (target && target != dock)
tabifyDockWidget(target, dock);
dock->raise();
}