Restarting an application
-
Hello,
I am having some problems implementing a "File/New" functionality into my app. That is when I want to start a project from scratch.
What I have is a main window and it has two widgets which are a 'mainDisplay' and a 'secondaryDisplay'. And then the Maindisplay has some more widgets of its own.
This is what I am basically trying to do (simplified a little bit), I don't know if this is the right way to go so I would appreciate any insight.
My constructor has:@
void MainWindow::MainWindow(QWidget* parent) :QMainWindow(parent)
{
m_statusBar = new QStatusBar(this);
setStatusBar(m_statusBar);m_menuBar = new MenuBar(this); // a class that inherits QMenu setMenuBar(m_menuBar); m_toolBar = new ToolBar(this); //a class that inherits QToolBar addToolBar(m_toolBar); m_centralWidget = new QWidget(this); m_mainDisplay = new MainDisplay(m_centralWidget); secondaryDisplay = new SecondaryDisplay(this); mainSplitter = new QSplitter(Qt::Horizontal, this); mainSplitter->addWidget(secondaryDisplay); mainSplitter->addWidget(m_centralWidget); QList<int> splitterWidths; splitterWidths.append(300); splitterWidths.append(1000); mainSplitter->setSizes(splitterWidths); setCentralWidget(mainSplitter);
}
@Then when I call File/New, I am simply doing this:
@
void MainWindow::closeSession()
{disconnectUi(); // simply disconnects all the widgets's connections, not sure if really needed //Widget Recreation delete m_statusBar; m_statusBar = new QStatusBar(this); setStatusBar(m_statusBar); delete m_menuBar; m_menuBar = new MenuBar(this); setMenuBar(m_menuBar); delete m_toolBar; m_toolBar = new ToolBar(this); addToolBar(m_toolBar); delete m_centralWidget; m_centralWidget = new QWidget(this); m_mainDisplay = new MainDisplay(m_centralWidget); delete secondaryDisplay; secondaryDisplay = new SecondaryDisplay(this); QList<int> splitterWidths; splitterWidths.append(300); splitterWidths.append(1000); for(int i = 0; i < splitterWidths.size(); i++) delete mainSplitter->widget(i); mainSplitter->addWidget(secondaryDisplay); mainSplitter->addWidget(m_centralWidget); mainSplitter->setSizes(splitterWidths); //Make all necessary connections connectUi(); // reconnects every connection previously made
}
@This is very unstable, sometimes it works just fine, sometimes it works launching the main window but without the toolbar and sometimes it crashes with seg fault or bus error or not a consistent reason. I realize it all depends on my widgets and maybe lots of other factors. But is this a good way to go to "restart" the app, what things should I be looking for that can be causing the instability and crashes? Any insight I would really appreciate.
Thanks.