Where to delete the object for modaless dialog Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Where to delete the object for modaless dialog

Post by Ronald » Thu Sep 05, 2019 12:58 pm

Code: Select all

MyDialog * pDlg = new MyDialog(...);
pDlg->Show();
Where to put delete this?

Neither MyDialog::~MyDialog nor OnDestroy(EVT_WINDOW_DESTROY) is called when the window is closed by click the close button.
Until the parent window (the main window for the app) is closed, OnDestroy and MyDialog::~MyDialog are called in order.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3408
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Where to delete the object for modaless dialog

Post by ONEEYEMAN » Thu Sep 05, 2019 2:24 pm

Hi,
Why do you need to create it on the heap?

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 13989
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Where to delete the object for modaless dialog

Post by doublemax » Thu Sep 05, 2019 2:37 pm

Neither MyDialog::~MyDialog nor OnDestroy(EVT_WINDOW_DESTROY) is called when the window is closed by click the close button.
Handle the wxEVT_CLOSE event.

However, if you need a modal window, use wxDialog. For non-modal, use wxFrame.
Why do you need a non-modal dialog?

Additionally, a wxDialog should not destroy itself as it could be created on the stack and there is no way for the dialog itself to know. The part in your application that creates the dialog should also destroy it.
Use the source, Luke!

Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Re: Where to delete the object for modaless dialog

Post by Ronald » Thu Sep 05, 2019 2:47 pm

ONEEYEMAN wrote:
Thu Sep 05, 2019 2:24 pm
Why do you need to create it on the heap?
Need a modaless dialog, after the function exits the dialog should be there.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3408
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Where to delete the object for modaless dialog

Post by ONEEYEMAN » Thu Sep 05, 2019 2:53 pm

Hi,
Which function?
Are you creating a thread behind this dialog?

What is the exact scenario?

As doublemaxc pointed out - it is better to use wxDialog for modality and wxFrame/wxPanel for modeless things.

But in any case - dialogs are very rarely created on the heap.

Thank you.

Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Re: Where to delete the object for modaless dialog

Post by Ronald » Thu Sep 05, 2019 2:55 pm

doublemax wrote:
Thu Sep 05, 2019 2:37 pm
Handle the wxEVT_CLOSE event.
delete this in OnClose crashes the program
doublemax wrote:
Thu Sep 05, 2019 2:37 pm
However, if you need a modal window, use wxDialog. For non-modal, use wxFrame.
Why do you need a non-modal dialog?
For updating data for a long time. I should use wxFrame.
doublemax wrote:
Thu Sep 05, 2019 2:37 pm
Additionally, a wxDialog should not destroy itself as it could be created on the stack and there is no way for the dialog itself to know. The part in your application that creates the dialog should also destroy it.
OK, I'm from MFC, there a dialog can be modal or modaless.

Thanks

Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Re: Where to delete the object for modaless dialog

Post by Ronald » Thu Sep 05, 2019 2:59 pm

ONEEYEMAN wrote:
Thu Sep 05, 2019 2:53 pm
Which function?
Are you creating a thread behind this dialog?
a button click event handler
ONEEYEMAN wrote:
Thu Sep 05, 2019 2:53 pm
What is the exact scenario?
crawl data from web, needs a long time.
ONEEYEMAN wrote:
Thu Sep 05, 2019 2:53 pm
As doublemaxc pointed out - it is better to use wxDialog for modality and wxFrame/wxPanel for modeless things.
But in any case - dialogs are very rarely created on the heap.
wxFrame is what I need here, I'm from MFC and wxDialog has a method Show, it makes me think it can be modal by design.

Thanks

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3408
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Where to delete the object for modaless dialog

Post by ONEEYEMAN » Thu Sep 05, 2019 3:07 pm

Hi,
So the scenario you are looking for is this:

1. You have a main top-level wxFrame with the button.
2. When this button is clicked, you start the web crawler in a thread and at the same time display the dialog.
3. When the crawler is done you dismiss the dialog and display the data acquired in the main top-level window.

Am I right?

In this case you should probably run wxProgressDialog during crawling.

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 13989
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Where to delete the object for modaless dialog

Post by doublemax » Thu Sep 05, 2019 3:41 pm

delete this in OnClose crashes the program
If you really want the dialog to destroy itself (i still think it's wrong), then calling Destroy() should probably work.
Use the source, Luke!

Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Re: Where to delete the object for modaless dialog

Post by Ronald » Thu Sep 05, 2019 4:02 pm

ONEEYEMAN wrote:
Thu Sep 05, 2019 3:07 pm
Hi,
So the scenario you are looking for is this:

1. You have a main top-level wxFrame with the button.
2. When this button is clicked, you start the web crawler in a thread and at the same time display the dialog.
3. When the crawler is done you dismiss the dialog and display the data acquired in the main top-level window.

Am I right?
When crawling data, the newest crawled data can be used by the application instantly.
ONEEYEMAN wrote:
Thu Sep 05, 2019 3:07 pm
In this case you should probably run wxProgressDialog during crawling.
Currently the dialog is quite customized:
1. contain a wxPropertyGrid for config crawler
2. contain a wxPropertyGrid for showing different summary status for the progress, such as: total tasks/left tasks/succeeded tasks/failed tasks
3. contain a list ctrl for show logging messages
Is wxProgressDialog suitable?

Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Re: Where to delete the object for modaless dialog

Post by Ronald » Thu Sep 05, 2019 4:06 pm

doublemax wrote:
Thu Sep 05, 2019 3:41 pm
delete this in OnClose crashes the program
If you really want the dialog to destroy itself (i still think it's wrong), then calling Destroy() should probably work.
I think so, now I used wxNotebook to contain a page for updating.
Thanks

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3408
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Where to delete the object for modaless dialog

Post by ONEEYEMAN » Thu Sep 05, 2019 5:32 pm

Hi,
The customization crawler dialog should probably be done as a modal dialog.
Now when the crawler setting is satisfactory, user presses OK, wxProgressDialog starts (probnably wxGenericProgressDialog) with some customizatrions - such as number of tasks completed/succeeded/failed, etc.

But both of them should be modal.

Thank you.

Ronald
Earned some good credits
Earned some good credits
Posts: 107
Joined: Mon Mar 05, 2018 4:17 am

Re: Where to delete the object for modaless dialog

Post by Ronald » Fri Sep 06, 2019 3:17 am

ONEEYEMAN wrote:
Thu Sep 05, 2019 5:32 pm
Hi,
The customization crawler dialog should probably be done as a modal dialog.
Now when the crawler setting is satisfactory, user presses OK, wxProgressDialog starts (probnably wxGenericProgressDialog) with some customizatrions - such as number of tasks completed/succeeded/failed, etc.
I've looked some screenshot about wxProgressDialog, it is designed for progress.
ONEEYEMAN wrote:
Thu Sep 05, 2019 5:32 pm
But both of them should be modal.
What does modal mean here?
If modal means parent window is not operatable, it can't be modal in the case, because data updating is a background job.
I think wxFrame is more suitable.
Thanks

Post Reply