Results 1 to 6 of 6

Thread: QWidget "before destroy"-like event.

  1. #1
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QWidget "before destroy"-like event.

    Hello all. In my application i have my saveSettings virtual method, that saves widget settings. This widgets using as MDI children, QDialog children and as standalone windows. What signal handler shoould I prefer to use to call my saveSettings? Destructors is not solution, because my method is virtual and call my virtual method from destructor will results undefined behaviour. loadSettings must be called AFTER child widgets are created, and loadSettings must be called BEFORE child widgets are destroyed. Here is my C++11 code:

    Qt Code:
    1. template<class T> class ICSWidget_Template: public T {
    2. protected:
    3. virtual void loadSettings() = 0;
    4. virtual void saveSettings() = 0;
    5.  
    6. virtual void showEvent(QShowEvent* pEvent) override {
    7. T::showEvent(pEvent);
    8.  
    9. if(!m_bInitialized) {
    10. loadSettings();
    11. m_bInitialized = true;
    12. }
    13. }
    14.  
    15. // Some stuff to call saveSettings?
    16.  
    17. private:
    18. bool m_bInitialized = false;
    19. };
    20.  
    21.  
    22. /*
    23.   This is usage of ICSWidget_Template
    24. */
    25. class ICSMainWindow: public ICSWidget_Template<QMainWindow> {
    26. // App-specific code
    27. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWidget "before destroy"-like event.

    May be something like when application is aboutToQuit ??
    Or may be when object is destroyed.. whichever suits your need

  3. #3
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget "before destroy"-like event.

    aboutToQuit useless in my app, cause these widgets destroys at runtime, not at application termination. I can's use signal-slot connection, because MOC does not support class templates (

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget "before destroy"-like event.

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget "before destroy"-like event.

    Quote Originally Posted by wysota View Post
    I know about this signal. How can i connect handler to this signal in template-specialized class?


    UPDATE: I've traced destruction process step-by step. It consists of following steps:

    1. Calling destructor of derived class
    2. Calling destructor of QWidget
    3. Emitting destroyed() signal

    IE, in destroyed() handler VMT already destructed and I cant call my virtual method correctly.

    It is correct behaviour, because destructors chaining from derived to base, until ~QObject reached. In this step destroyed() signal emitting. SO i need some signal or virtual function, that executing before QWidget-derived class destructor executed. In Windows this is WM_DESTROY message. In Qt i've found member function QWidget::destroy, but it isn't virtual, so I cannot use this
    Last edited by golubevsv; 30th September 2014 at 14:44.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget "before destroy"-like event.

    Use composition instead of inheritance to achieve virtualization.

    e.g.
    Qt Code:
    1. class SettingsHandlerBase {
    2. public:
    3. virtual ~SettingsHandlerBase() {}
    4. virtual void saveSettings() = 0;
    5. virtual void loadSettings() = 0;
    6. };
    7.  
    8. template<class T> SettingsHandler {
    9. public:
    10. SettingsHandler(T *object) : m_object(object) {}
    11. void saveSettings() { m_object->saveSettings(); }
    12. void loadSettings() { m_object->loadSettings(); }
    13. };
    14.  
    15. class X : public SettingsHandler<X> {
    16. public:
    17. X(...) : SettingsHandler<X>(this) {}
    18. void saveSettings() { /* do your stuff */ };
    19. void loadSettings() { /* do your stuff */ };
    20. };
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. class SettingsSubject;
    2. class SettingsHandler {
    3. public:
    4. virtual ~SettingsHandler() {}
    5. virtual void saveSettings(SettingsSubject *) = 0;
    6. virtual void loadSettings(SettingsSubject *) = 0;
    7. };
    8.  
    9. class SettingsSubject {
    10. public:
    11. SettingsSubject(SettingsHandler *h) {
    12. m_settingsHandler = h;
    13. m_settingsHandler->loadSettings(this);
    14. }
    15. ~SettingsSubject() { m_settingsHandler->saveSettings(this); delete m_settingsHandler; }
    16. };
    17.  
    18. class Something : public SettingsSubject {
    19. public:
    20. Something() : SettingsSubject(new SomethingSettingsHandler) {}
    21. };
    To copy to clipboard, switch view to plain text mode 

    or something in a similar fashion.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 20th September 2010, 10:58
  2. "new" + "delete" a QWidget and its children
    By vkincaid in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 22:51
  3. Replies: 2
    Last Post: 18th November 2009, 04:43
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QWidget pixmap AFTER displaying html ("rich text")
    By gavrilo princep in forum Newbie
    Replies: 0
    Last Post: 17th July 2007, 02:59

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.