QT - Soft close MDI child from its own code

Hi,
I have a mdi child in my QT app and now I want to close this softly if an error happen. Means, I want to throw a message and then close the child. Currently I have the problem that it do not close the MDI window, it still is inside the MDI area.
Any hint to close the own mdi from its own code?

Thanks,

Ingo
Ingo FoersterProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
What is your MDI child window based from? There are different ways of implementing the mdi child windows in QT
0
Ingo FoersterProgrammerAuthor Commented:
class MdiChildDiskInfo : public MdiChildBase
class MdiChildBase : public QMainWindow
0
chaauCommented:
You need to call this->close().
However please note that you need to check the code in your closeEvent(). The code needs to accept the event.
void MdiChildDiskInfo::closeEvent(QCloseEvent *event)
{
      MdiChildBase::closeEvent(event);
      event->accept();
}

Open in new window

If you have already any code in this event and you need to signal that it is indeed the intended close event you can introduce a boolean variable in the MdiChildDiskInfo class:
MdiChildDiskInfo: public MdiChildBase
{
private:
bool closeAnyway;
}

Open in new window

Make this variable default to false in the constructor of MdiChildDiskInfo. Then in your closing code set it to true before calling this->close(), like this:
closeAnyway = true;
this->close();

Open in new window

Then modify the closeEvent to test this variable:
void MdiChildDiskInfo::closeEvent(QCloseEvent *event)
{
      MdiChildBase::closeEvent(event);
      if(closeAnyway) {
           event->accept();
      } else {
         // your existing code here
      }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.