Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

closeEvent doesn't work



  • Hi, I try to do this :

    void MainWindow::closeEvent(QCloseEvent *event)
    {
    	qDebug("close");
    	writeSettingsForMainWindow();
    	event->accept();
    }
    

    And in my class MainWindow :

    protected:
        void closeEvent(QCloseEvent *event) override;
    

    But never I enter in my closeEvent.
    I don't see my problem...
    Someone see it ?



  • Ok I see, thanks a lot
    I'm going to try this

    Edit :
    Ok thanks it's working :)


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    On what OS ?
    How are you closing your window ?

    Can you show the rest of your MainWindow implementation ?



  • Hi, I'm using Qt 5.10 in Windows 10
    I am closing my window with the cross.
    the rest of my MainWindow :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include "tabbedwindow.h"
    #include "boxtheme.h"
    #include <QtWidgets>
    
    class MainWindow : public QMainWindow
    {
    	Q_OBJECT
    
    public:
    	MainWindow(void);
    	~MainWindow(void);
    	QMainWindow *window;
    	TabbedWindow *createTabbedWindow(void);
    	QVector<TabbedWindow *> tabbed;
    
    public slots:
    	void addSplitPanelV(void);
    	void addSplitPanelH(void);
    	void chooseTheme(void);
    	void addTabPanel(void);
    
    protected:
    	void closeEvent(QCloseEvent *event) override;
    
    private:
    	void addThisTabPanel(TabbedWindow *window);
    	void addSplitPanelVAtThesesTabbedWindow(TabbedWindow *first, TabbedWindow *second);
    	void addSplitPanelHAtThesesTabbedWindow(TabbedWindow *first, TabbedWindow *second);
    	void createMainMenu(void);
    	void createMainAction(void);
    	void writeSettingsForMainWindow(void);
    	void readSettingsForMainWindow(void);
    	int searchIndexOfThisTabbedWindow(TabbedWindow *window);
    	void stockThisSplitter(TabbedWindow *first, QString splitter, TabbedWindow *second);
    	void applySplitters(void);
    	QWidget *widget;
    	QVBoxLayout *layout;
    	QMenu *menuFile;
    	QMenu *menuWindow;
    	QMenu *menuTools;
    	QMenu *menuHelp;
    	QAction *actTheme;
    	QAction *actExit;
    	QAction *actAddTabPanel;
    	QAction *actAddSplitPanelV;
    	QAction *actAddSplitPanelH;
    	QAction *actAboutQt;
    	QVector<QString> splitters;
    	BoxTheme *boxTheme;
    };
    
    #endif
    

  • Lifetime Qt Champion

    Why do you have a QMainWindow member in your MainWindow class ?



  • Yeah I know that I can remove it. I do it later ^^


  • Lifetime Qt Champion

    Then show your implementation code.

    Because from the looks of it, right now, you might be trying to go through the closeEvent from you MainWindow class, but you might be showing your window member variable and not your MainWindow object.



  • ok, this is MainWindow.cpp

    #include "mainwindow.h"
    
    void MainWindow::applySplitters(void)
    {
    	QSettings settings("settings.ini", QSettings::IniFormat);
    
    	for (int i = 0; i < splitters.size(); i += 1) {
    		QStringList list = splitters[i].split(" ");
    		QString first = list[0];
    		QString second = list[2];
    
    		if (list[1] == "vertical")
    			addSplitPanelVAtThesesTabbedWindow(tabbed[first.toInt()], tabbed[second.toInt()]);
    		else
    			addSplitPanelHAtThesesTabbedWindow(tabbed[first.toInt()], tabbed[second.toInt()]);
    		splitters.pop_back();
    	}
    }
    
    void MainWindow::readSettingsForMainWindow()
    {
    	QSettings settings("settings.ini", QSettings::IniFormat);
    	int nbrTabbedWindow = settings.value("MainWindow/nbrTabbedWindow").toInt();
    	int nbrSplitters = settings.value("MainWindow/nbrSplitters").toInt();
    
    	boxTheme->list->setCurrentText(settings.value("App/theme").toString());
    	//	BoxTheme::apply();
    	window->restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
    	window->restoreState(settings.value("MainWindow/state").toByteArray());
    	for (int i = 0; i < nbrSplitters; i += 1) {
    		QString path = "MainWindow/splitters/" + QString::number(i);
    
    		splitters.push_back(settings.value(path.toStdString().c_str()).toString());
    	}
    	for (int i = 0; i < nbrTabbedWindow; i += 1) {
    		QString path = "MainWindow/TabbedWindow/" + QString::number(i);
    
    		if (i > 0)
    			createTabbedWindow();
    		settings.beginGroup(path.toStdString().c_str());
    		tabbed[i]->splitter->restoreGeometry(settings.value("splitter/geometry").toByteArray());
    		tabbed[i]->containerWidget->restoreGeometry(settings.value("containerWidget/geometry").toByteArray());
    		int sizeWindow = settings.value("nbrWindows").toInt();
    		for (int j = 0; j < sizeWindow; j += 1) {
    			QString pathWindowGeo = "/windows" + QString::number(j) + "/geometry";
    			QString pathWindowSta = "/windows" + QString::number(j) + "/state";
    			QString pathContentTextGeo = "/contents" + QString::number(j) + "/text/geometry";
    			QString pathContentTextTxt = "/contents" + QString::number(j) + "/text/text";
    			QString pathContentWidgetGeo = "/contents" + QString::number(j) + "/widget/geometry";
    			QString pathContentWidgetColor = "/contents" + QString::number(j) + "/widget/color";
    			QString pathContentWidgetColorUsage = "/contents" + QString::number(j) + "/widget/colorUsage";
    
    			if (j > 0)
    				addThisTabPanel(tabbed[i]);
    			tabbed[i]->windows[j]->restoreGeometry(settings.value(pathWindowGeo.toStdString().c_str()).toByteArray());
    			tabbed[i]->windows[j]->restoreState(settings.value(pathWindowSta.toStdString().c_str()).toByteArray());
    			tabbed[i]->contents[j]->text->restoreGeometry(settings.value(pathContentTextGeo.toStdString().c_str()).toByteArray());
    			tabbed[i]->contents[j]->text->setText(settings.value(pathContentTextTxt.toStdString().c_str()).toString());
    			tabbed[i]->contents[j]->widget->restoreGeometry(settings.value(pathContentWidgetGeo.toStdString().c_str()).toByteArray());
    			tabbed[i]->contents[j]->color = settings.value(pathContentWidgetColor.toStdString().c_str()).value<QColor>();
    			tabbed[i]->contents[j]->colorUsage = settings.value(pathContentWidgetColorUsage.toStdString().c_str()).value<QColor>();
    			applyAtThisWidgetThisStylesheet(tabbed[i]->contents[j]->widget, tabbed[i]->contents[j]->color);
    		}
    		settings.endGroup();
    	}
    	applySplitters();
    	putFocusAtTrue(tabbed[tabbed.size() - 1], tabbed);
    }
    
    void MainWindow::writeSettingsForMainWindow()
    {
    	QSettings settings("settings.ini", QSettings::IniFormat);
    
    	settings.setValue("App/theme", boxTheme->list->currentText());
    	settings.setValue("MainWindow/geometry", window->saveGeometry());
    	settings.setValue("MainWindow/state", window->saveState());
    	settings.setValue("MainWindow/nbrSplitters", splitters.size());
    	// Boucle pour sauvegarder l'ordre des splitters
    	for (int i = 0; i < splitters.size(); i += 1) {
    		QString path = "MainWindow/splitters/" + QString::number(i);
    
    		settings.setValue(path.toStdString().c_str(), splitters[i]);
    	}
    	settings.setValue("MainWindow/nbrTabbedWindow", tabbed.size());
    	// Boucle pour sauvegarder TabbedWindow
    	for (int i = 0; i < tabbed.size(); i += 1) {
    		QString path = "MainWindow/TabbedWindow/" + QString::number(i);
    
    		settings.beginGroup(path.toStdString().c_str());
    		settings.setValue("splitter/geometry", tabbed[i]->splitter->saveGeometry());
    		settings.setValue("containerWidget/geometry", tabbed[i]->containerWidget->saveGeometry());
    		settings.setValue("nbrWindows", tabbed[i]->windows.size());
    		// Boucle pour sauvegarder Tabs in TabbedWindow
    		for (int j = 0; j < tabbed[i]->windows.size(); j += 1) {
    			QString pathWindowGeo = "/windows" + QString::number(j) + "/geometry";
    			QString pathWindowSta = "/windows" + QString::number(j) + "/state";
    			QString pathContentTextGeo = "/contents" + QString::number(j) + "/text/geometry";
    			QString pathContentTextTxt = "/contents" + QString::number(j) + "/text/text";
    			QString pathContentWidgetGeo = "/contents" + QString::number(j) + "/widget/geometry";
    			QString pathContentWidgetColor = "/contents" + QString::number(j) + "/widget/color";
    			QString pathContentWidgetColorUsage = "/contents" + QString::number(j) + "/widget/colorUsage";
    
    			settings.setValue(pathWindowGeo.toStdString().c_str(), tabbed[i]->windows[j]->saveGeometry());
    			settings.setValue(pathWindowSta.toStdString().c_str(), tabbed[i]->windows[j]->saveState());
    			settings.setValue(pathContentTextGeo.toStdString().c_str(), tabbed[i]->contents[j]->text->saveGeometry());
    			settings.setValue(pathContentTextTxt.toStdString().c_str(), tabbed[i]->contents[j]->text->text());
    			settings.setValue(pathContentWidgetGeo.toStdString().c_str(), tabbed[i]->contents[j]->widget->saveGeometry());
    			settings.setValue(pathContentWidgetColor.toStdString().c_str(), tabbed[i]->contents[j]->color);
    			settings.setValue(pathContentWidgetColorUsage.toStdString().c_str(), tabbed[i]->contents[j]->colorUsage);
    		}
    		settings.endGroup();
    	}
    }
    
    void MainWindow::closeEvent(QCloseEvent *event)
    {
    	qDebug("close");
    	writeSettingsForMainWindow();
    	event->accept();
    }
    
    void MainWindow::createMainMenu(void)
    {
    	menuFile = window->menuBar()->addMenu(tr("&File"));
    	menuFile->addAction(actNew);
    	menuFile->addAction(actOpen);
    	menuFile->addSeparator();
    	menuFile->addAction(actExit);
    
    	menuWindow = window->menuBar()->addMenu(tr("&Window"));
    	menuWindow->addAction(actAddTabPanel);
    	menuWindow->addAction(actAddSplitPanelV);
    	menuWindow->addAction(actAddSplitPanelH);
    
    	menuTools = window->menuBar()->addMenu(tr("&Tools"));
    	menuTools->addAction(actTheme);
    
    	menuHelp = window->menuBar()->addMenu(tr("&Help"));
    	menuHelp->addAction(actAboutQt);
    }
    
    void MainWindow::createMainAction(void)
    {
    	actNew = new QAction(tr("&New"));
    	actNew->setShortcut(QKeySequence("Ctrl+N"));
    	actNew->setStatusTip(tr("New"));
    	//	connect(actNew, SIGNAL(triggered()), this, SLOT(new()));
    
    	actOpen = new QAction(tr("&Open"));
    	actOpen->setShortcut(QKeySequence("Ctrl+O"));
    	actOpen->setStatusTip(tr("Open a file"));
    	connect(actOpen, SIGNAL(triggered()), this, SLOT(open()));
    
    	actExit = new QAction(tr("&Exit"));
    	actExit->setShortcut(QKeySequence("Ctrl+Q"));
    	actExit->setStatusTip(tr("Exit the application"));
    	connect(actExit, SIGNAL(triggered()), qApp, SLOT(quit()));
    
    	actAddTabPanel = new QAction(tr("Add &Tab Panel"));
    	actAddTabPanel->setShortcut(QKeySequence("Ctrl+T"));
    	actAddTabPanel->setStatusTip(tr("Add a new window"));
    	connect(actAddTabPanel, SIGNAL(triggered()), this, SLOT(addTabPanel()));
    
    	actAddSplitPanelV = new QAction(tr("Add Split Panel &Vertical"));
    	actAddSplitPanelV->setShortcut(QKeySequence("Ctrl+V"));
    	actAddSplitPanelV->setStatusTip(tr("Add a new window"));
    	connect(actAddSplitPanelV, SIGNAL(triggered()), this, SLOT(addSplitPanelV()));
    
    	actAddSplitPanelH = new QAction(tr("Add Split Panel &Horizontal"));
    	actAddSplitPanelH->setShortcut(QKeySequence("Ctrl+H"));
    	actAddSplitPanelH->setStatusTip(tr("Add a new window"));
    	connect(actAddSplitPanelH, SIGNAL(triggered()), this, SLOT(addSplitPanelH()));
    
    	actAboutQt = new QAction(tr("About &Qt"), this);
    	actAboutQt->setStatusTip(tr("Show the Qt library's About box"));
    	connect(actAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    
    	actTheme = new QAction(tr("Choose a &theme"));
    	actTheme->setStatusTip(tr("Choose between different theme"));
    	connect(actTheme, SIGNAL(triggered()), this, SLOT(chooseTheme()));
    }
    
    void MainWindow::chooseTheme(void)
    {
    	boxTheme->exec();
    }
    
    void MainWindow::openThisPathWithThisSuffix(QString suffix, QString path)
    {
    	if (suffix == "txt")
    		createTextOnThisContent(path, searchCurrentContentPanel(searchTabbedSelected(tabbed)));
    	else if (suffix == "png" || suffix == "jpg")
    		createImageOnThisContent(path, searchCurrentContentPanel(searchTabbedSelected(tabbed)));
    }
    
    void MainWindow::open(void)
    {
    	QString path = QFileDialog::getOpenFileName(this, "Select a file", QDir::homePath());
    	QFileInfo info(path);
    
    	openThisPathWithThisSuffix(info.suffix(), path);
    }
    
    TabbedWindow *searchTabbedSelected(QVector<TabbedWindow *> windows)
    {
    	for (int i = 0; i < windows.size(); i = i + 1)
    		if (windows[i]->focus)
    			return (windows[i]);
    	return (NULL);
    }
    
    TabbedWindow *MainWindow::createTabbedWindow(void)
    {
    	TabbedWindow *window = new TabbedWindow;
    	ContentPanel *content = new ContentPanel();
    	QMainWindow *first = new QMainWindow;
    
    	window->tabbed = &tabbed;
    	window->splitter = new QSplitter;
    	window->containerWidget = new QWidget;
    	window->containerLayout = new QVBoxLayout;
    	window->containerLayout->setMargin(0);
    
    	window->splitter->addWidget(window);
    	window->containerLayout->addWidget(window->splitter);
    	window->containerWidget->setLayout(window->containerLayout);
    	first->setCentralWidget(content->widget);
    	window->addView(first, "first");
    
    	putFocusAtTrue(window, tabbed);
    	window->contents.push_back(content);
    	window->windows.push_back(first);
    	MainWindow::tabbed.push_back(window);
    	return (window);
    }
    
    void MainWindow::addThisTabPanel(TabbedWindow *window)
    {
    	if (!window)
    		window = searchTabbedSelected(tabbed);
    	if (!window)
    		return;
    	ContentPanel *content = new ContentPanel();
    	QMainWindow *other = new QMainWindow;
    
    	other->setCentralWidget(content->widget);
    	window->addView(other, "other");
    
    	window->contents.push_back(content);
    	window->windows.push_back(other);
    
    	window->setCurrentView(window->windows.size() - 1);
    }
    
    void MainWindow::addTabPanel(void)
    {
    	addThisTabPanel(NULL);
    }
    
    int MainWindow::searchIndexOfThisTabbedWindow(TabbedWindow *window)
    {
    	for (int i = 0; i < tabbed.size(); i += 1)
    		if (tabbed[i] == window)
    			return (i);
    	return (-1);
    }
    
    void MainWindow::stockThisSplitter(TabbedWindow *first, QString splitter, TabbedWindow *second)
    {
    	int firstNb = searchIndexOfThisTabbedWindow(first);
    	int secondNb = searchIndexOfThisTabbedWindow(second);
    	QString newSplit = QString::number(firstNb) + " " + splitter + " " + QString::number(secondNb);
    
    	splitters.push_back(newSplit);
    }
    
    void addSplitAtOther(TabbedWindow *first, TabbedWindow *second)
    {
    	QSplitter *tmp = second->splitter;
    
    	second->splitter->insertWidget(0, first);
    	first->splitter->insertWidget(0, second->containerWidget);
    
    	first->splitter = tmp;
    	second->splitter = new QSplitter;
    	second->containerWidget = new QWidget;
    	second->containerLayout = new QVBoxLayout;
    	second->containerLayout->setMargin(0);
    	second->splitter->addWidget(second);
    	second->containerLayout->addWidget(second->splitter);
    	second->containerWidget->setLayout(second->containerLayout);
    	tmp->addWidget(second->containerWidget);
    }
    
    void MainWindow::addSplitPanelVAtThesesTabbedWindow(TabbedWindow *first, TabbedWindow *second)
    {
    	if (!first)
    		first = searchTabbedSelected(tabbed);
    	if (!first)
    		return;
    	if (!second)
    		second = createTabbedWindow();
    	if (first->splitter->children().size() == 2)
    		first->splitter->addWidget(second->containerWidget);
    	else
    		addSplitAtOther(first, second);
    	stockThisSplitter(first, "vertical", second);
    }
    
    void MainWindow::addSplitPanelHAtThesesTabbedWindow(TabbedWindow *first, TabbedWindow *second)
    {
    	if (!first)
    		first = searchTabbedSelected(tabbed);
    	if (!first)
    		return;
    	if (!second)
    		second = createTabbedWindow();
    	if (first->splitter->children().size() == 2) {
    		first->splitter->setOrientation(Qt::Vertical);
    		first->splitter->addWidget(second->containerWidget);
    	} else {
    		second->splitter->setOrientation(Qt::Vertical);
    		addSplitAtOther(first, second);
    	}
    	stockThisSplitter(first, "horizontal", second);
    }
    
    void MainWindow::addSplitPanelV(void)
    {
    	addSplitPanelVAtThesesTabbedWindow(NULL, NULL);
    }
    
    void MainWindow::addSplitPanelH(void)
    {
    	addSplitPanelHAtThesesTabbedWindow(NULL, NULL);
    }
    
    MainWindow::MainWindow(void)
    {
    	boxTheme = new BoxTheme();
    	widget = new QWidget;
    	window = new QMainWindow;
    	layout = new QVBoxLayout;
    
    	createMainAction();
    	createMainMenu();
    
    	createTabbedWindow();
    
    	layout->addWidget(tabbed[0]->containerWidget);
    	layout->setMargin(0);
    	widget->setLayout(layout);
    
    	window->setCentralWidget(widget);
    	window->statusBar()->showMessage("");
    	window->setWindowTitle("NVR");
    	window->setMinimumSize(1280, 960);
    	window->resize(1280, 960);
    	window->show();
    	readSettingsForMainWindow();
    }
    
    MainWindow::~MainWindow(void)
    {
    	writeSettingsForMainWindow();
    	delete boxTheme;
    	delete menuFile;
    	delete menuWindow;
    	delete menuTools;
    	delete menuHelp;
    	delete actExit;
    	delete actAddTabPanel;
    	delete actAddSplitPanelV;
    	delete actAddSplitPanelH;
    	delete actAboutQt;
    	delete actTheme;
    	for (int i = 0; i < tabbed.size(); i += 1)
    		delete tabbed[i];
    	delete layout;
    	delete widget;
    	delete window;
    }
    

  • Lifetime Qt Champion

    Your code confirms what I suspected: you are not using your MainWindow , but the QMainWindow * member variable of your MainWindow class. Therefore, your closeEvent method is never called because you never show your MainWindow instance.

    Why don't you setup MainWindow rather than that member variable ?



  • Ok I see, thanks a lot
    I'm going to try this

    Edit :
    Ok thanks it's working :)


Log in to reply