From 6b2326d0b61d588a7b9d1a8a1b2ca434cda1799f Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 8 Jan 2018 15:34:11 +0100 Subject: Fixed bugs with QDockWidgets - QDockWidgets are now deleted on close - Opening a new BookmarksWidget no longer leaves an empty dock widget - Fixed crash with deleting dock widgets --- src/mainwindow.cpp | 56 +++++++++++++++++++++++++++++++++++------------------- src/mainwindow.h | 5 ++--- 2 files changed, 38 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a2b9ff1..f57cab9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -7,24 +7,18 @@ */ #include "mainwindow.h" +#include "browser.h" #include "forms/aboutdialog.h" +#include "forms/searchform.h" #include "ui_mainwindow.h" #include "widgets/mainwindowmenubar.h" -#include - #include - -#include - +#include #include #include - -#include "browser.h" -#include - #include - -#include "forms/searchform.h" +#include +#include MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) : QMainWindow(parent) @@ -93,7 +87,8 @@ MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) url.replace("$term", t); tabBar->currentView()->load(QUrl::fromUserInput(url)); }); - connect(tabBar, SIGNAL(currentTabChanged(WebView *)), this, SLOT(handleTabChanged(WebView *))); + connect(tabBar, &MainWindowTabBar::currentTabChanged, this, &MainWindow::handleTabChanged); + //connect(tabBar, SIGNAL(currentTabChanged(WebView *)), this, SLOT(handleTabChanged(WebView *))); // loading bar ui->statusBar->addPermanentWidget(m_progressBar); @@ -136,12 +131,17 @@ MainWindow::~MainWindow() void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QWidget *widget) { - // get all dock widgets - QList allDockWidgets = findChildren(); - // make a list of widgets in the area we want + // this way we can append the new dock widget to the last one QVector areaDockWidgets; - for(QDockWidget *w : allDockWidgets) { + for(QDockWidget *w : findChildren()) { + // check if widget is already shown + if(w->widget() == widget) { + // in this case, close the dock and return + w->close(); + return; + } + if(dockWidgetArea(w) == area) { areaDockWidgets.append(w); } @@ -149,13 +149,29 @@ void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QWidget *widget) // create a dock widget QDockWidget *dock = new QDockWidget(widget->windowTitle(), this); - - // release the held widget when deleted - connect(dock, &QDockWidget::destroyed, this, [dock]() { - dock->widget()->setParent(nullptr); + dock->setAttribute(Qt::WA_DeleteOnClose, true); + + // when the dock widget becomes invisble, release the docked widget + // setting the widget makes the dock its parent; setting parent back to nullptr + // makes the dock not show the widget any more + connect(dock, &QDockWidget::visibilityChanged, [dock](bool visible) { + if(!visible && dock->widget()) { + dock->widget()->setParent(nullptr); + } }); dock->setWidget(widget); + // the same widget may be shown by docks in other windows + // in that case, they grab ownership and the current dock won't be showing anything + // so the current widget needs to be closed + auto *w = dynamic_cast(widget); + w->closeOthers(); + if(w) { + connect(w, &BookmarksWidget::closeOthersSignal, dock, [dock]() { + dock->close(); + }); + } + if(areaDockWidgets.empty()) { // no other widgets addDockWidget(area, dock); diff --git a/src/mainwindow.h b/src/mainwindow.h index c62ebe3..8febb71 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -43,7 +43,8 @@ class MainWindow : public QMainWindow friend class MainWindowMenuBar; public: - MainWindow(std::shared_ptr config, QWidget *parent = nullptr); + explicit MainWindow(std::shared_ptr config, QWidget *parent = nullptr); + Q_DISABLE_COPY(MainWindow) ~MainWindow() override; void addTabbedDock(Qt::DockWidgetArea area, QWidget *widget); @@ -71,8 +72,6 @@ private slots: void handleTitleUpdated(const QString &title); private: - Q_DISABLE_COPY(MainWindow) - Ui::MainWindow *ui; SearchForm *m_searchBox; -- cgit v1.2.1