Results 1 to 2 of 2

Thread: Do something in mainwindow when a sub window closes

  1. #1
    Join Date
    Jan 2017
    Posts
    54
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Do something in mainwindow when a sub window closes

    I have a Qmainwindow that has several buttons, which when clicked, open other windows. When one of these sub windows is closed using the red X, I would like to set focus to a particular QLineEdit in the main window, which remains open. I do not want to do any processing in the sub window when it closes, but simply want to do something in the still-open main window. So far:

    In one of the sub windows, in the init, I have:
    Qt Code:
    1. self.closeEvent(self.mainWindow.reset_focus)
    To copy to clipboard, switch view to plain text mode 

    In the mainwindow, I have:
    Qt Code:
    1. def reset_focus(self):
    2. self.ui.xyz.setFocus()
    3. self.ui.xyz.selectAll()
    To copy to clipboard, switch view to plain text mode 

    I thought I could just set up an event handler in the init of the sub window, but the above does not work. I've read:
    https://www.qtcentre.org/threads/610...=close%20event
    in this forum, but cannot determine how to translate to my situation, nor can I read the C code.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Do something in mainwindow when a sub window closes

    self.closeEvent(self.mainWindow.reset_focus)
    QWidget::closeEvent() is an event called by the Qt event loop when a widget window is closed. You can't redefine it to do something else.

    One solution is to define a custom signal in your child widget class that is emitted when the window receives a close event. Override closeEvent for your child widget, and in it emit the signal.

    When you create the child widget in the main window, connect a slot in your main window class to the signal from the child class. When the child window is closed, it will emit the signal, which will call the slot in your main window. Do whatever you want in the slot.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    dennisvz (22nd June 2021)

Similar Threads

  1. Replies: 1
    Last Post: 20th December 2012, 08:59
  2. New window closes automatically
    By Gavin Harper in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2010, 15:43
  3. execute sth before program exits (user closes window)
    By donglebob in forum Qt Programming
    Replies: 6
    Last Post: 10th December 2008, 00:24
  4. QMdiSubWindow closes main window
    By frido in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2008, 11:54
  5. QTextBrowser - click on link - closes window
    By bruccutler in forum Newbie
    Replies: 2
    Last Post: 19th June 2007, 21:39

Tags for this Thread

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.