Results 1 to 6 of 6

Thread: Python / PyQt5 MDI Window focus problem

  1. #1
    Join Date
    Sep 2015
    Location
    Montevideo, UY
    Posts
    4
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Python / PyQt5 MDI Window focus problem

    I'm making my firsts steps into python/pyqt5 world, and trying to develop an application. The app has an MDI approach, and the main functionality is already accomplished and seems to work fine (up to now).

    A subwindow that contains a database table (T1) is opened at the very beginning of the app, from this window another subwindow that contains a dialog (D1) can be opened to enter record data to this table.
    After this, i added a development almost similar in behaviour as the described above (a window that contains a table (T2) from which a dialog is opened (D2) to enter record data).

    The point is that the window that containts T2 and the window that contains D2 seems to have focus at the same time.
    At this point, if D2 is closed the window D2 AND T2 are closed.

    If T1 is clicked (taking focus away of D2 or T2) and then D2 is clicked again focus is taken by D2 correctly, and if D2 is closed ... it closes as expected.

    I will post some code for your guidance.

    Qt Code:
    1. class MainWindow(QMainWindow):
    2.  
    3. def __init__(self, parent=None):
    4. super(MainWindow, self).__init__(parent)
    5.  
    6. self.setObjectName("MainWindow")
    7.  
    8. self.centralwidget = QWidget(self)
    9. self.centralwidget.setObjectName("centralwidget")
    10.  
    11. self.mdiArea = QMdiArea(self.centralwidget)
    12. self.mdiArea.setObjectName("mdiArea")
    13. self.mdiArea.setActivationOrder(QMdiArea.ActivationHistoryOrder)
    14.  
    15. self.setCentralWidget(self.mdiArea)
    16.  
    17. globvar.mdiArea = self.mdiArea
    To copy to clipboard, switch view to plain text mode 
    globvar is a module that contains global variables (mdiarea, winlist). winlist is an instance of the class WindowList that let us control not to open twice the same window. This class has two methods, add and remove both return a boolean reporting the success status.

    Here is the call of the T1 -> ImpABM made from the MainWindow class.

    Qt Code:
    1. def ImpABM(self):
    2. if globvar.winList.add('MdiIMP'):
    3. child = modimpo.MdiIMP(self)
    4. globvar.mdiArea.addSubWindow(child)
    5. child.show()
    To copy to clipboard, switch view to plain text mode 

    And the call of the T2 --> ProvABM from the MainWindow class

    Qt Code:
    1. def CliABM(self):
    2. if globvar.winList.add('ABMfmcli'):
    3. tabla = modabm.tabla( "fmcli", "Clientes")
    4. tabla.addField(modabm.campo("cliid", "N", 6, "Código", "999999"))
    5. tabla.addField(modabm.campo("clidesc", "C", 60, "Descripción" ))
    6. tabla.isSequence("cliid", True)
    7. tabla.orden = "clidesc"
    8.  
    9. self.cli = modabm.abmMaestros( self, tabla )
    10. globvar.mdiArea.addSubWindow( self.cli )
    11. self.cli.setPos(20, 20)
    12. self.cli.setSize(500, 200)
    13. self.cli.cargaDatos()
    14. self.cli.show()
    To copy to clipboard, switch view to plain text mode 
    In module file modimpo, the class MdiIMP is defined as follow

    Qt Code:
    1. class MdiImp(QDialog):
    2.  
    3. def __init__(self, parent=None):
    4. super(MdiImp, self).__init__(parent)
    5. self.setAttribute(Qt.WA_DeleteOnClose)
    6. self.objectName = "MdiImp"
    7. ...
    8.  
    9. def closeEvent(self, event):
    10. globvar.winList.remove( 'MdiImp' )
    To copy to clipboard, switch view to plain text mode 

    In the class MdiIMP a call to D1 -> dialgImpo is made.

    Qt Code:
    1. def ImpoAlta(self):
    2. if globvar.winList.add( 'dialgImpo' ):
    3. d = dialgImpo( self, "A" )
    4. globvar.mdiArea.addSubWindow( d )
    5. d.updTabla.connect( self.LoadTabla )
    6. d.show()
    To copy to clipboard, switch view to plain text mode 
    The dialgImpo class is as follow :

    Qt Code:
    1. class dialgImpo(QDialog):
    2.  
    3. updTabla = pyqtSignal()
    4.  
    5. def __init__(self, parent=None, accion="" ):
    6. super(dialgImpo, self).__init__(parent)
    7. ...
    8. self.cancela = QtGui.QPushButton(self)
    9. self.cancela.setObjectName("cancela")
    10. self.cancela.clicked.connect( globvar.mdiArea.closeActiveSubWindow )
    11. ...
    12.  
    13. def closeEvent( self, event ):
    14. globvar.winList.remove( 'dialgImpo' )
    15. globvar.mdiArea.closeActiveSubWindow()
    To copy to clipboard, switch view to plain text mode 
    Up to now, seems to work fine.

    Having in mind the idea of creating a master file maintenance class, i've developed the modabm module that contains the tabla (table) class that has a list of instances of campo (field) class, also defined in this module. The tabla class creates a table view of a master file based on a list of fields, as seen above in the ProvABM method.

    Qt Code:
    1. class tabla(QDialog):
    2.  
    3. lista = ()
    4.  
    5. def __init__(self, parent=None, nom="", desc=""):
    6. super(tabla, self).__init__(parent)
    7. self.setAttribute(Qt.WA_DeleteOnClose)
    8. self.nombre = nom
    9. ...
    10.  
    11. def closeEvent(self, event):
    12. globvar.winList.remove('ABM' + self.nombre)
    To copy to clipboard, switch view to plain text mode 
    This window closes properly (till here everything is fine).
    In the tabla class there is a method that creates a dialog based on the field definition to enter the record data.
    Qt Code:
    1. def AltaRec(self):
    2. if globvar.winList.add('dialgABM' + self.nombre):
    3. debug("-- Dialogo ABM AltaRec " + self.nombre)
    4. capturafm = dialgABM(self, ABM_ALTA, self.desc, self.campos, self.dialgx, self.dialgy, self.nombre)
    5. globvar.mdiArea.addSubWindow(capturafm)
    6. capturafm.show()
    To copy to clipboard, switch view to plain text mode 
    Here is the definition of D2 -> dialgABM class (the one that is creating problems).

    Qt Code:
    1. class dialgABM(QtGui.QDialog):
    2.  
    3. commitRec = QtCore.Signal()
    4.  
    5. def __init__(self, parent=None, accion='', titulo='', campos=[], x=0, y=0, nombre=''):
    6. super(dialgABM, self).__init__(parent)
    7. ...
    8. self.cancela.clicked.connect(globvar.mdiArea.closeActiveSubWindow)
    9. ...
    10.  
    11. def closeEvent(self, event):
    12. globvar.mdiArea.closeActiveSubWindow()
    13. globvar.winList.remove('dialgABM' + self.nomtabla)
    To copy to clipboard, switch view to plain text mode 

    When closing the windows using the X button D2 AND T2 are closed.
    When pressing the cancela button, T2 is closed.
    When pressing the confirma button, T2 is closed too.
    It seems that the T2 table is the "current" window when D2 is closed.
    Tried (with no success) to setActiveWindow the D2 dialog.

    Any help to solve this issue will be appreciated. I have not posted all the code because doesn't make sense.
    If someone could be interested in this development, please let me know, there is no problem sharing it.

    Thanks in advance
    Last edited by apereira; 14th September 2015 at 05:17. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Python / PyQt5 MDI Window focus problem

    You are closig the "active sub window" of the mdi area.
    That will always be an mdi sub window, not any dialog.

    Btw, in you create a widget as "central widget" but then don't use it as such.
    Just create the mdi area.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Location
    Montevideo, UY
    Posts
    4
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: Python / PyQt5 MDI Window focus problem

    Quote Originally Posted by anda_skoa View Post
    You are closig the "active sub window" of the mdi area.
    That will always be an mdi sub window, not any dialog._
    As i could understand from the docs, the next lines creates a subwindow in the mdiarea containing the dialog.

    Qt Code:
    1. capturafm = dialgABM(self, ABM_ALTA, self.desc, self.campos, self.dialgx, self.dialgy, self.nombre)
    2. globvar.mdiArea.addSubWindow(capturafm)
    To copy to clipboard, switch view to plain text mode 

    You are suggesting to create an mdisubwindow and then activate it?
    dialgABM should return a QMdiSubWindow object and then globvar.mdiarea.setActiveSubwindow( capturafm )


    Quote Originally Posted by anda_skoa View Post
    Btw, in you create a widget as "central widget" but then don't use it as such.
    Just create the mdi area.

    Cheers,
    _
    Ok, noted and corrected.

    Many thanks for your help.

    Best regards.

    Ariel Pereira

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Python / PyQt5 MDI Window focus problem

    I am afraid I don't understand.

    If you want a QMdiArea subwindow, why derive from QDialog?
    You only do that if you want a dialog.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2015
    Location
    Montevideo, UY
    Posts
    4
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: Python / PyQt5 MDI Window focus problem

    Dear anda_skoa,

    Thanks for your message.

    I've changed the class from QDialog to QWidget, but still having the same behaviour.
    For your guidance, find a preview.jpg file where it is shown the behaviour of the app (both windows "Clientes" and "Clientes > Nuevo" have focus at the same time).

    I'm also attaching a test file (mainwindow.ui) created with QtCreator, and a p.py file created with pyuic5, with two added lines that links the QWidgets to the mdiarea.
    As I can understand from this sample, the subwindows are created in the addSubWindow mehtod, and they contain a QWidget object.
    I've been following the idea of this file into the app but still having the same result.

    Any help will be welcome.

    Regards.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by apereira; 18th September 2015 at 18:26.

  6. #6
    Join Date
    Sep 2015
    Location
    Montevideo, UY
    Posts
    4
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: Python / PyQt5 MDI Window focus problem

    Got it!!

    It was a setFocus call that was causing this problem ... don't know exactly why ... but after comenting the line the window focus problem has gone.
    The setFocus was used to force the focus to be on a certain QLineEdit, but it seems that something else is going on.

    Thanks for your help.


    Regards.

Similar Threads

  1. Replies: 0
    Last Post: 26th July 2015, 04:45
  2. Replies: 3
    Last Post: 14th February 2012, 15:47
  3. Embedding PyQt4 into an Application running Python via Boost::Python
    By GreyHound in forum Installation and Deployment
    Replies: 1
    Last Post: 6th February 2012, 06:48
  4. Window Focus Problem
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2010, 13:34
  5. Window focus issues (How to force focus to a window?)
    By montylee in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2009, 01:00

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.