Quit on last window closed

in Qt there is a very useful signal lastWindowClosed which can be used to
terminate its one application if the last window is going to be closed:

QObject::connect(qApp,SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));

Sometimes this would be nice to have in some specific ROOT applications.
Is something similar possible in ROOT, without using QtROOT?

Hi suter_a,
signal-slots are implemented in ROOT.
Check root.cern.ch/root/HowtoSignalSlot.html

Superiour than Qt’s:

  • no extension of C++ syntacsis
  • no MOC preprocessor
  • work in interpreted as well as in compiled code
  • slot can be any class method
  • slot can be function
  • slot can have default parameters
  • allows, so called, class connection, i.e.
    connect signal from any object of some calss to some slot.
    This is true “dynamic overloading of class method”
    (no subclassing, no recompiling)

Regards. Valeriy

Hi,
I overlooked the question itself :frowning: .
There is TGMainFrame::CloseWindow signal which is emitted
when top level window (main frame) is closed either
programmally or via Alt-F4 or via “close button”.
If you have several main frames in your application …
no solution … it would be easy done if the list of all registed windows
(TGClient::fWlist) would be “available”. We will add this getter.

Thanks. Regards. Valeriy

Hi suter_a,
TGClient::GetListOfWindows is now in CVS.
Back to your question …
now it can be done something like this

void printLastWindowClosed()
{
    if (gClient->GetListOfWindows()->IsEmpty()) {
         printf("Closed!\n");
    }
}

Put somewhere in your code

   TQObject::Connect("TGMainFrame", "CloseWindow()", 
                     0, 0, "printLastWindowClosed()");

That establishes “class connection” from signal CloseWindow()
emitted by any main frame to the handler function
printLastWindowClosed()

Regards. Valeriy

Hi suter_a,
small correction to printLastWindowClosed()
because at the time “CloseWindow” signal is emitted
the list of frames is not empty yet.

void printLastWindowClosed() 
{
     TIter next(gClient->GetListOfWindows());
     TObject *o;
     Int_t nMainFrames = 0;

     while ((o=next())) {
        if (o->InheritsFrom(TGMainFrame::Class()))
            nMainFrames++;
     } 
     if (nMainFrames <=1) {
         printf("Closed!\n"); 
    } 
}

Hi Valeriy,

I tried to follow your advice but stumbled already very early with
the following very simple program:


#include

using namespace std;

#include <TApplication.h>
#include <TCanvas.h>

void lastCanvasClosed() {
cout << endl << “have been in lastCanvasClosed()” << endl;
}

int main(int argc, char *argv[])
{
TApplication myApp(“myapp”,0,0);
TCanvas *c1, *c2;

TQObject::Connect(“TCanvas”, “Closed()”, 0, 0, “lastCanvasClosed()”);

c1 = new TCanvas(“c1”,“c1 …”,520,10,400,300);
c2 = new TCanvas(“c2”,“c2 …”,110,10,400,300);
myApp.Run();

return 1;
}


It compiles nicely, but when started it complains

Error in TQOBject::CheckConnectArgs: slot lastCanvasClosed() does
not exist. :cry:

ROOT does not see the lastCanvasClosed() function, and I have no
clue how I can getting into it. As soon as this problem is fixed, your suggestion is exactly what I would need.

Thanks Andreas

Hi Andreas,
lastCanvasClosed() should be global or
interpreted function. So, you need to pass it through
the rootcint root.cern.ch/root/CintGenerator.html

Regards. Valeriy

Thanks a lot, now it works :laughing: