Thanks to d_stranz and anda_skoa, I was able to solve my problem :P Here is how i did it.
frmMain represents the Qmainwindow.
frmTest is the extra window I want to open and close. The variable for this is oTestFrame.
frmMain.h
...
private:
frmTest * oTestFrame ; //how frmTest should be defined. This is a pointer.
private slots:
void onClosed();
...
...
private:
frmTest * oTestFrame ; //how frmTest should be defined. This is a pointer.
private slots:
void onClosed();
...
To copy to clipboard, switch view to plain text mode
frmMain.cpp
{
ui.setupUi(this);
oTestFrame = new frmTest(); //for the first time, we initialize the object in the frmMain constructor. Now we can check if a form already exists or not
}
void frmMain::on_btnOpenNew_clicked() //this can be anything. For this case, a button was used.
{
if (!oTestFrame->isActiveWindow()) //only one window can exist at all time.
{
oTestFrame->show();
QObject::connect(oTestFrame,
SIGNAL(destroyed
()),
this,
SLOT(onClosed
()));
}
}
void frmMain::onClosed()
{
QObject::disconnect(oTestFrame,
SIGNAL(destroyed
()),
this,
SLOT(onClosed
()));
//I don't really know for sure if this is necessary, after destruction, but hey, you never know oTestFrame = new frmTest(); //ready a new form te be opened. All previous changes are lost, which was the point.
}
frmMain::frmMain(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
oTestFrame = new frmTest(); //for the first time, we initialize the object in the frmMain constructor. Now we can check if a form already exists or not
}
void frmMain::on_btnOpenNew_clicked() //this can be anything. For this case, a button was used.
{
if (!oTestFrame->isActiveWindow()) //only one window can exist at all time.
{
oTestFrame->show();
QObject::connect(oTestFrame, SIGNAL(destroyed()), this, SLOT(onClosed()));
}
}
void frmMain::onClosed()
{
QObject::disconnect(oTestFrame, SIGNAL(destroyed()), this, SLOT(onClosed())); //I don't really know for sure if this is necessary, after destruction, but hey, you never know
oTestFrame = new frmTest(); //ready a new form te be opened. All previous changes are lost, which was the point.
}
To copy to clipboard, switch view to plain text mode
frmTest.cpp : not a lot extra code is needed to let this work
{
ui.setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose, true); // This makes sure the form is destroyed after it is closed.
}
frmTest::frmTest(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose, true); // This makes sure the form is destroyed after it is closed.
}
To copy to clipboard, switch view to plain text mode
Bookmarks