Re: Control Closing of Gtk::Dialog



Murray Cumming wrote:
On Mon, 2009-02-16 at 13:35 +0100, Jon Black wrote:
Murray Cumming wrote:
On Sun, 2009-02-15 at 15:51 +0100, Jon Black wrote:
I have a simple Gtk::Dialog with 3 text entry fields, a cancel button,
and an ok button. I'd like the dialog to close whenever the user
presses cancel, and when the user presses ok only if all the text
entry fields contain text. It seems that by default, the dialog closes
when either button is pressed.
No, you should need to explicitly hide() it or delete it. But if you use
Dialog::run(), run() will return when one of the buttons is pressed.
Maybe the dialog is just going out of scope when Dialog::run() returns.
I think it is going out of scope after run is called. When I get home
I'll try and keep it in scope and see what happens. Since it is
returning, though, does that mean I'd have to re-run the dialog?

You could do that. Or you could just use show() and handle the response
signal. And/or make buttons insensitive when they should not be clicked.

If I connect the buttons to a handler, there seems to be no way to
prevent the closing from happening. So, how can I control whether the
dialog closes when the ok button is pressed?
I think you can override on_response() to prevent run() from returning.
I forget the details.
The dialog is created with glade, so how can I override a function?
Would I need to create a class myself that derives from Dialog, and
then use that class in my call to get_widget()?

You could do that.

Ok, I managed to get it working. I'm sending this so other people can get it working. This may or may not be the correct way to do it.

Firstly, the dialog was going out of scope. I changed it by creating a pointer to the dialog. This means the dialog stays in scope until the pointer is deleted. I had assumed that calling response actually returned from the dialog, but it appears this sets the response, which is then returned when the dialog is hidden. So, I set the desired response and call hide(), which returns the dialog. I handle the response, and after processing any values I want from the dialog, call delete on the dialog pointer. In the dialog, I only call hide() when the user has entered correct information.

I imagine there might be a slightly better (non-pointer) way to do this, but it works and I want to get on with other tasks :)

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]