Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to run myfunction() when I close a window
Forum Updated to NodeBB v4.3 + New Features

how to run myfunction() when I close a window

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 331 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • blackout69B Offline
    blackout69B Offline
    blackout69
    wrote on last edited by
    #1

    Hi everyone,
    I need to run myfunction() when I close the window named mywindow programmatically opened inside the FullScreen class

    .h
    namespace Ui {
      class FullScreen;
    }
    
    class FullScreen : public QWidget
    {
      Q_OBJECT
    
    public:
      explicit FullScreen(QWidget *parent = nullptr);
      ~FullScreen();
    
    private:
      Ui::FullScreen *ui;
      QWindow *mywindow;
    
    private slots:
      void on_button_released();
      void myfunction();
    };
    
    .cpp
    
    void FullScreen::on_button_released()
    {
       WId wid = (WId)FindWindow(L"ScreenClass", nullptr);
       mywindow = QWindow::fromWinId(wid);
    
    // I tried like this, but it doesn't work   
    QObject::connect(mywindow, &QWindow::windowStateChanged, this, [this](Qt::WindowState state){
           if (state == Qt::WindowNoState && mywindow->isVisible() == false) {
               myfunction();
           }
       });
    }
    
    void FullScreen::myfunction()
    {
      my code;
    }
    

    Does anyone have a different solution?
    Thank you in advance
    blackout69

    JonBJ 1 Reply Last reply
    0
    • blackout69B blackout69

      Hi everyone,
      I need to run myfunction() when I close the window named mywindow programmatically opened inside the FullScreen class

      .h
      namespace Ui {
        class FullScreen;
      }
      
      class FullScreen : public QWidget
      {
        Q_OBJECT
      
      public:
        explicit FullScreen(QWidget *parent = nullptr);
        ~FullScreen();
      
      private:
        Ui::FullScreen *ui;
        QWindow *mywindow;
      
      private slots:
        void on_button_released();
        void myfunction();
      };
      
      .cpp
      
      void FullScreen::on_button_released()
      {
         WId wid = (WId)FindWindow(L"ScreenClass", nullptr);
         mywindow = QWindow::fromWinId(wid);
      
      // I tried like this, but it doesn't work   
      QObject::connect(mywindow, &QWindow::windowStateChanged, this, [this](Qt::WindowState state){
             if (state == Qt::WindowNoState && mywindow->isVisible() == false) {
                 myfunction();
             }
         });
      }
      
      void FullScreen::myfunction()
      {
        my code;
      }
      

      Does anyone have a different solution?
      Thank you in advance
      blackout69

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @blackout69
      To run code on closing a QWidget, either subclass and override closeEvent() or detect it via installEventFilter() (avoids subclassing, but here you are already subclassing).

      B 1 Reply Last reply
      1
      • JonBJ JonB

        @blackout69
        To run code on closing a QWidget, either subclass and override closeEvent() or detect it via installEventFilter() (avoids subclassing, but here you are already subclassing).

        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

        @JonB Since the OP's window is got by QWindow::fromWinId (probably from a non-Qt program's window handle), I doubt he could get any information according to the doc of the function:

        Note: The resulting QWindow should not be used to manipulate the underlying native window (besides re-parenting), or to observe state changes of the native window. Any support for these kind of operations is incidental, highly platform dependent and untested.

        JonBJ 1 Reply Last reply
        0
        • B Bonnie

          @JonB Since the OP's window is got by QWindow::fromWinId (probably from a non-Qt program's window handle), I doubt he could get any information according to the doc of the function:

          Note: The resulting QWindow should not be used to manipulate the underlying native window (besides re-parenting), or to observe state changes of the native window. Any support for these kind of operations is incidental, highly platform dependent and untested.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Bonnie
          Yes, I did not notice that was what the OP was asking about. Assuming that nothing like closeEvent() or installEventFilter() works I assume they need either to use purely non-Qt native code or perhaps Qt's nativeEventFilter()?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved