Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2008
Catching 'X' Button Close Event with QCloseEvent.


Message 1 in thread

Hi,

I am quite a newbie using QT. I have a QDialog which I created using Qt 
Designer. The dialog presents some options to be selected by the user. 
The Dialog is called by a QWidget and the widget is part of QMainWindow. 
For calling/creating this dialog object I am using the UiLoader approach.

On this Dialog I have 2 push buttons, clicking one of them passes the 
options selected by the user in the dialog to the parent widget (sort of 
  'accept') and the other is sort of 'reject' button. The slot 
accepthing the clicked() signals from both these buttons closes the 
dialog and if it is the 'reject' button then even the parent widget is 
closed by the slot function.

My problem is that if the user presses the 'X' button on the Dialog 
window (instead of the push buttons) the dialog simply closes with none 
of the 'accept' or 'reject' type of effect. How does catch the 'X' 
button press event ?

  If I want to implement the QCloseEvent then how do I know if the user 
has pressed the 'X' button. Is there anyway to know whether the 'X' 
button has been pressed?

Secondly, since I am using the UiLoader approach to show the dialog, how 
do I implement the QCloseEvent method as there is no class for the 
dialog that is created.

I hope I have been able to explain the situation properly.

TIA,
Rajen.

--
 [ signature omitted ] 

Message 2 in thread

I doubt there is a cross-platform way to know if the window has been
closed by clicking on the X or by pressing e.g. ALT+F4 if this is what
you want.

Use close events to intercept a window is being closed.
The closeEvent(QCloseEvent*) method will be called by Qt as soon as
the user tries to close the window using the usual window manager
features (X button or keyboard shortcuts).

Use keyPress events if you are using a QDialog and want to intercept
the ESC key (which will usually close the dialog).

Regards,
Fabrizio


2008/3/4, Rajen M. Parekh <help.lists@xxxxxxxxx>:
> Hi,
>
>  I am quite a newbie using QT. I have a QDialog which I created using Qt
>  Designer. The dialog presents some options to be selected by the user.
>  The Dialog is called by a QWidget and the widget is part of QMainWindow.
>  For calling/creating this dialog object I am using the UiLoader approach.
>
>  On this Dialog I have 2 push buttons, clicking one of them passes the
>  options selected by the user in the dialog to the parent widget (sort of
>   'accept') and the other is sort of 'reject' button. The slot
>  accepthing the clicked() signals from both these buttons closes the
>  dialog and if it is the 'reject' button then even the parent widget is
>  closed by the slot function.
>
>  My problem is that if the user presses the 'X' button on the Dialog
>  window (instead of the push buttons) the dialog simply closes with none
>  of the 'accept' or 'reject' type of effect. How does catch the 'X'
>  button press event ?
>
>   If I want to implement the QCloseEvent then how do I know if the user
>  has pressed the 'X' button. Is there anyway to know whether the 'X'
>  button has been pressed?
>
>  Secondly, since I am using the UiLoader approach to show the dialog, how
>  do I implement the QCloseEvent method as there is no class for the
>  dialog that is created.
>
>  I hope I have been able to explain the situation properly.
>
>  TIA,
>  Rajen.
>
>
>  --
>  To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
>  List archive and information: http://lists.trolltech.com/qt-interest/
>
>

--
 [ signature omitted ] 

Message 3 in thread

Hi Rajen,

sorry I did not remember you were dynamically loading a form :-)
You can use event filters, I suppose.
Events going to the widget will be first sent to your class. Please
look at the docs for installEventFilter() and eventFilter(), the
following code is just quick example:

MyQObjectClass::someMethod() {

       // Your code here
       QUiLoader loader;
       QFile file("editpurchase.ui");
       file.open(QFile::ReadOnly);
       QWidget *widget = loader.load(&file, this);

       // HERE IT COMES :)
       widget->installEventFilter(this);
       // ...
}

MyQObjectClass::eventFilter(QEvent* e, QObject* o) {
   if (o == MY_WIDGET && e->type() == QEvent::CLoseEvent) {
    // HANDLE EVENT....
  }
}

Hope this helps :)

Regards,
Fabrizio


2008/3/6, Rajen M. Parekh <help.lists@xxxxxxxxx>:
> Fabrizio Angius wrote:
>  > I doubt there is a cross-platform way to know if the window has been
>  > closed by clicking on the X or by pressing e.g. ALT+F4 if this is what
>  > you want.
>
>  Yes... thanks for clearing my doubts.
>
>  > Use close events to intercept a window is being closed.
>  > The closeEvent(QCloseEvent*) method will be called by Qt as soon as
>  > the user tries to close the window using the usual window manager
>  > features (X button or keyboard shortcuts).
>  >
>  > Use keyPress events if you are using a QDialog and want to intercept
>  > the ESC key (which will usually close the dialog).
>
>  As mentioned in my earlier post, I would like to implement the
>  closeEvent method, but since the dialog is created directly using
>  QUiLoader and not a class by itself, I am not sure how to go about it.
>  Here is how I am creating the dialog form.
>
>         QUiLoader loader;
>         QFile file("editpurchase.ui");
>         file.open(QFile::ReadOnly);
>         QWidget *widget = loader.load(&file, this);
>         file.close();
>         optionsDialog = static_cast<QDialog*>(widget);
>         ui_individualComboBox =
>  optionsDialog->findChild<QComboBox*>("individual_comboBox");
>         ...
>         ...
>         ui_selectPushButton
>  =optionsDialog->findChild<QPushButton*>("select_pushButton");
>         ui_cancelPushButton
>  =optionsDialog->findChild<QPushButton*>("cancel_pushButton");
>         connect(ui_cancelPushButton, SIGNAL(clicked()), this, SLOT(close()));
>         connect(ui_cancelPushButton, SIGNAL(clicked()), optionsDialog,
>  SLOT(close()));
>         connect(ui_selectPushButton, SIGNAL(clicked()), this, SLOT(startEdit()));
>
>  How do I implement the closeEvent method with dynamically created objects?
>
>  TIA,
>
> Rajen.
>
>

--
 [ signature omitted ] 

Message 4 in thread

Fabrizio Angius wrote:
>        // HERE IT COMES :)
>        widget->installEventFilter(this);
>        // ...
> }
> 
> MyQObjectClass::eventFilter(QEvent* e, QObject* o) {
>    if (o == MY_WIDGET && e->type() == QEvent::CLoseEvent) {
>     // HANDLE EVENT....
>   }
> }
> 
> Hope this helps :)

Thank-you Fabrizio. Works like charm. :)

Regards,
Rajen.

--
 [ signature omitted ] 

Message 5 in thread

Rajen,

If I understand correctly what you're trying to do, your problem is that
clicking the 'x' of the dialog does not do any of the dialog rejection
code you've written.  However, if you move that code to the parent, you
can do the following:

void MyParent::showDialog()
{
  MyDialog d;
  if (d.exec() == QDialog::Rejected) {
    // do rejection processing
    // then close the parent
    close();
  } else {
    // dialog accepted, do that processing
  }
}

Then all you have to do in your dialog is connect the clicked() slot of
one button to the QDialog::accept() slot, and connect the other to the
QDialog::reject() slot.  The 'x' automagically counts as rejection.

Hope that helps.

Darrik

Rajen M. Parekh wrote:
> Hi,
> 
> I am quite a newbie using QT. I have a QDialog which I created using Qt
> Designer. The dialog presents some options to be selected by the user.
> The Dialog is called by a QWidget and the widget is part of QMainWindow.
> For calling/creating this dialog object I am using the UiLoader approach.
> 
> On this Dialog I have 2 push buttons, clicking one of them passes the
> options selected by the user in the dialog to the parent widget (sort of
>  'accept') and the other is sort of 'reject' button. The slot accepthing
> the clicked() signals from both these buttons closes the dialog and if
> it is the 'reject' button then even the parent widget is closed by the
> slot function.
> 
> My problem is that if the user presses the 'X' button on the Dialog
> window (instead of the push buttons) the dialog simply closes with none
> of the 'accept' or 'reject' type of effect. How does catch the 'X'
> button press event ?
> 
>  If I want to implement the QCloseEvent then how do I know if the user
> has pressed the 'X' button. Is there anyway to know whether the 'X'
> button has been pressed?
> 
> Secondly, since I am using the UiLoader approach to show the dialog, how
> do I implement the QCloseEvent method as there is no class for the
> dialog that is created.
> 
> I hope I have been able to explain the situation properly.
> 
> TIA,
> Rajen.
> 
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 
> 


-- 
 [ signature omitted ] 

Message 6 in thread

darrik mazey wrote:
> void MyParent::showDialog()
> {
>   MyDialog d;
>   if (d.exec() == QDialog::Rejected) {
>     // do rejection processing
>     // then close the parent
>     close();
>   } else {
>     // dialog accepted, do that processing
>   }
> }
> 
> Then all you have to do in your dialog is connect the clicked() slot of
> one button to the QDialog::accept() slot, and connect the other to the
> QDialog::reject() slot.  The 'x' automagically counts as rejection.

Well, I tried to do it the way you have explained above but closing the 
dialog by clicking the 'X' button does not count it as rejection. 
Anyway, as mentioned in my earlier post, I had solved it by using 
eventFilter as explained by Fabrizio in his earlier post. But still, 
thanks for you reply.

Regards,
Rajen.

--
 [ signature omitted ]