Results 1 to 6 of 6

Thread: Python / PyQt5 MDI Window focus problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2015
    Montevideo, UY
    Qt products
    Qt5 PyQt3 PyQt4

    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):
    3. def __init__(self, parent=None):
    4. super(MainWindow, self).__init__(parent)
    6. self.setObjectName("MainWindow")
    8. self.centralwidget = QWidget(self)
    9. self.centralwidget.setObjectName("centralwidget")
    11. self.mdiArea = QMdiArea(self.centralwidget)
    12. self.mdiArea.setObjectName("mdiArea")
    13. self.mdiArea.setActivationOrder(QMdiArea.ActivationHistoryOrder)
    15. self.setCentralWidget(self.mdiArea)
    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)
    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"
    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()
    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):
    3. def __init__(self, parent=None):
    4. super(MdiImp, self).__init__(parent)
    5. self.setAttribute(Qt.WA_DeleteOnClose)
    6. self.objectName = "MdiImp"
    7. ...
    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 )
    To copy to clipboard, switch view to plain text mode 
    The dialgImpo class is as follow :

    Qt Code:
    1. class dialgImpo(QDialog):
    3. updTabla = pyqtSignal()
    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. ...
    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):
    3. lista = ()
    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. ...
    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)
    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):
    3. commitRec = QtCore.Signal()
    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. ...
    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

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


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.