Results 1 to 6 of 6

Thread: Exit button that does a task before exiting

  1. #1
    Join Date
    Aug 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Exit button that does a task before exiting

    Good day.

    I'm trying to create a exit button that executes a few functions before closing. Therefore I can not use

    connect(exitButton,SIGNAL(clicked()),this, SLOT(quit()));

    since this close the program directly.

    Does anyone know how to write a customized quit() function or something else that can be used to execute a few functions and then close the handle of the program.

    Any help would greatly be appreciated

    thanx

  2. #2
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exit button that does a task before exiting

    You can try to write a destructor. This is automaticly called when you try to close your program and it would look like this:

    Qt Code:
    1. MyClass::~MyClass()
    2. {
    3. //your code...
    4. }
    To copy to clipboard, switch view to plain text mode 

    This way your code is automaticly executed right before the program closes.

    Good luck!

  3. #3
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Exit button that does a task before exiting

    1. You can do your desired in closeEvent.
    2. you can connect the exit button with your own slot. Do whatever you want, at the end call exit() function in that slot.
    3. Do same in destructor.

  4. #4
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Exit button that does a task before exiting

    Connect clicked() to you own slot that calls your "few functions" and ends with calling quit(). (Slots can be called like ordinary methods)

  5. #5
    Join Date
    Aug 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exit button that does a task before exiting

    Thanx for the help. I forgot about destructors.

    DrDonut how would you define the destructor in a class? I tried the following:

    class Controller : public QWidget
    {
    Q_OBJECT

    public:
    Controller(QWidget *parent = 0);
    ~Controller();

    but receives the following error:
    error LNK2019: unresolved external symbol "public: void __thiscall Controller::exit(void)" (?exit@Controller@@QAEXXZ) referenced in function "public: virtual int __thiscall Controller::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Controller@@UAEHW4Call@QMetaObject@@ HPAPAX@Z) moc_CONTROLLER.obj

    Do you have any idea?

  6. #6
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exit button that does a task before exiting

    Hi Roelof,

    As far as I know, it should work the way you put it in your header file.

    Did you moc your header file in the proper way? (That would explain the metaObject stuff in the error message).

    Good luck!

Similar Threads

  1. How to hide application button on the task bar?
    By QCasper in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 23:10
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Window without a exit button
    By jbpvr in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2007, 23:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.