Last active
December 9, 2023 21:53
-
-
Save aneury1/62a82da1e9da15b6ddcdb431a3b71c85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QMainWindow> | |
#include <QMenuBar> | |
#include <QMdiArea> | |
#include <QMdiSubWindow> | |
#include <QTextEdit> | |
#include <QVBoxLayout> | |
#include <QPushButton> | |
#include <QSignalMapper> | |
#include <QMainWindow> | |
#include <QToolBar> | |
#include <QAction> | |
#include <QTextEdit> | |
#include <QSplitter> | |
#include <QToolBar> | |
#include <QMessageBox> | |
#include <QTabBar> | |
class WidgetContainer: public QSplitter{ | |
public: | |
WidgetContainer(QWidget *parent): QSplitter(parent){ | |
// Create a vertical layout for the main window | |
QVBoxLayout mainLayout(this); | |
this->setOrientation(Qt::Horizontal); | |
// Create widgets to be added to the splitter | |
QTextEdit *textEdit1 = new QTextEdit; | |
QTextEdit *textEdit2 = new QTextEdit; | |
// Add widgets to the splitter | |
addWidget(textEdit1); | |
addWidget(textEdit2); | |
// Create a button for toggling collapsibility | |
QPushButton toggleButton("Toggle Collapsibility"); | |
// Connect the button's clicked signal to a slot that toggles the collapsibility | |
QObject::connect(&toggleButton, &QPushButton::clicked, [this]() { | |
// Toggle the collapsibility of the second handle | |
setCollapsible(1, !this->isCollapsible(1)); | |
}); | |
setSizes(QList<int>() << 100 << 600); | |
} | |
}; | |
class MainWindow : public QMainWindow { | |
/// Q_OBJECT | |
QTabBar* tabBar; | |
QMdiArea* mdiArea; | |
QVBoxLayout *centralLayout = new QVBoxLayout(); | |
void updateTabBar() { | |
// Clear existing tabs | |
tabBar->removeTab(0); | |
// Add tabs for each MDI child window | |
QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList(); | |
for (QMdiSubWindow *subWindow : subWindows) { | |
tabBar->addTab(subWindow->windowTitle()); | |
} | |
} | |
void closeTab(int index) { | |
QMdiSubWindow *subWindow = mdiArea->subWindowList().at(index); | |
if (subWindow) { | |
subWindow->close(); | |
} | |
} | |
void createMdiChild(const QString &title) { | |
QTextEdit *textEdit = new QTextEdit; | |
QMdiSubWindow *subWindow = mdiArea->addSubWindow(textEdit); | |
subWindow->setWindowTitle(title); | |
subWindow->show(); | |
// Update the tab bar | |
updateTabBar(); | |
} | |
void addMdiChild() { | |
static int childNumber = 2; | |
createMdiChild("Document " + QString::number(childNumber)); | |
++childNumber; | |
} | |
void createInstanceOfCenterLayout(){ | |
centralLayout = new QVBoxLayout(); | |
tabBar = new QTabBar(this); | |
connect(tabBar, &QTabBar::tabCloseRequested, this, &MainWindow::closeTab); | |
mdiArea = new QMdiArea(this); | |
connect(mdiArea, &QMdiArea::subWindowActivated, this, &MainWindow::updateTabBar); | |
} | |
void configureLayout(){ | |
// Add the tab bar and MDI area to the central layout | |
centralLayout->addWidget(tabBar); | |
centralLayout->addWidget(mdiArea); | |
// Create a central widget to hold the layout | |
QWidget *centralWidget = new QWidget(this); | |
centralWidget->setLayout(centralLayout); | |
setCentralWidget(centralWidget); | |
} | |
void addAction(){ | |
// Create an "Add MDI Child" action | |
QAction *addAction = new QAction("Add MDI Child", this); | |
connect(addAction, &QAction::triggered, this, &MainWindow::addMdiChild); | |
// Add the action to the toolbar | |
QToolBar *navToolBar = addToolBar("Navigation"); | |
navToolBar->addAction(addAction); | |
// Create actions for managing MDI child windows | |
QAction *newAction = new QAction("New", this); | |
QAction *cascadeAction = new QAction("Cascade", this); | |
QAction *tileAction = new QAction("Tile", this); | |
// Connect actions to slots | |
connect(newAction, &QAction::triggered, this, &MainWindow::createNewChild); | |
connect(cascadeAction, &QAction::triggered, mdiArea, &QMdiArea::cascadeSubWindows); | |
connect(tileAction, &QAction::triggered, mdiArea, &QMdiArea::tileSubWindows); | |
// Add actions to the toolbar | |
navToolBar->addAction(newAction); | |
navToolBar->addAction(cascadeAction); | |
navToolBar->addAction(tileAction); | |
} | |
public: | |
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { | |
createInstanceOfCenterLayout(); | |
configureLayout(); | |
addAction(); | |
} | |
private slots: | |
void createNewChild() { | |
// Create a new QTextEdit as an MDI child | |
auto *window = new WidgetContainer(this); | |
QMdiSubWindow *subWindow = mdiArea->addSubWindow(window); | |
// Set window properties | |
subWindow->setAttribute(Qt::WA_DeleteOnClose); | |
subWindow->setWindowTitle("Document"); | |
subWindow->show(); | |
} | |
private: | |
}; | |
int main(int argc, char *argv[]) { | |
QApplication app(argc, argv); | |
MainWindow mainWindow; | |
mainWindow.setWindowTitle("test of MDI"); | |
mainWindow.show(); | |
return app.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment