QWizard Field Looses Focus when In MDISubWindow -- don't want this.



  • I have a QWizard and inside of it is a QLineEdit. The finished button on the wizard is enabled when the number of characters in the QLineEdit is even. However, every time the enable/disable state of the Finish button is changed, the QLineEdit looses focus. How do I prevent this from happening?

    Examining this issue under the hood, it appears that when the enable/disable state of Finished is changed, the QLineEdit item briefly is hidden and then shown. While it is hidden, the focus is set to the Finish button since its the next widget in the tab order when the QLineEdit is visible.

    This behavior is only occurring when my QWizard is part of an MDIArea/subwindow. When the QWizard stands on its own, the user can happily type into the QLineEdit and the Finish button becomes enabled/disabled while the QLineEdit stays in focus.

    Below is a simple test program which demonstrates the issue.

    Thanks in advance.

    @import glob
    import os
    import sys
    from PyQt4 import Qt, QtGui, QtCore
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import PyQt4.Qt

    This lot (Wizard and WizardPage) works great.

    class Wizard(QWizard):
    def init(self, parent = None, flags = Qt.Widget):
    QWizard.init(self, parent, flags)

    class WizardPage(QWizardPage):
    def init(self, parent = None):
    QWizardPage.init(self, parent)
    layout = QVBoxLayout()
    self._lineEdit = QLineEdit('hank', self)
    self._lineEdit.textChanged.connect(self.onLineEditTextChanged)
    #self.registerField("lineEdit*", self._lineEdit)
    layout.addWidget(self._lineEdit)
    self.setLayout(layout)

    def isComplete(self):
        return(0 == len(self._lineEdit.text()) % 2)
    
    def onLineEditTextChanged(self, text):
        self.completeChanged.emit()
    

    def main2():
    app = QtGui.QApplication(sys.argv)

    wizard = Wizard()
    wizard.addPage(WizardPage(wizard))
    
    
    wizard.show()
    
    sys.exit(app.exec_())
    

    #-------------------------------------------------------------------------------

    Testing lot

    __count = 0

    def print2(text):
    print(str(__count) + ": " + text)
    global __count
    __count = __count + 1

    class LineEdit(QLineEdit):
    def init(self, contents, parent = None):
    QLineEdit.init(self, contents, parent)
    def event(self, event):
    print2("LineEdit: " + str(event) + " " + str(event.type()))
    return QLineEdit.event(self, event)
    def hideEvent(self, event):
    event.accept()
    self.show()
    return

    class NextButton(QPushButton):
    def init(self, parent = None):
    QPushButton.init(self, parent)
    def event(self, event):
    print2("NextButton: " + str(event) + " " + str(event.type()))
    return QPushButton.event(self, event)

    class Wizard2(QWizard):
    def init(self, parent = None, flags = Qt.Widget):
    QWizard.init(self, parent, flags)
    self._lineEdit = LineEdit('hank', self)
    self.setButton(QWizard.NextButton, NextButton(self))
    def event(self, event):
    print2("Wizard2: " + str(event) + " " + str(event.type()))
    return QWizard.event(self, event)

    class WizardPage2(QWizardPage):
    def init(self, parent = None):
    QWizardPage.init(self, parent)
    self._initialParent = parent
    layout = QVBoxLayout()
    parent._lineEdit.textChanged.connect(self.onLineEditTextChanged)
    layout.addWidget(parent._lineEdit)
    self.setLayout(layout)

    def isComplete(self):
        return(0 == len(self._initialParent._lineEdit.text()) % 2)
    
    def onLineEditTextChanged(self, text):
        self.completeChanged.emit()
    def event(self, event):
        print2("WizardPage2: " + str(event) + " " + str(event.type()))
        return QWizardPage.event(self, event)
    

    class MainWindow(QMainWindow):
    def init(self, parent = None, flags = Qt.WindowFlags(0)):
    QMainWindow.init(self, parent, flags)
    def event(self, event):
    print2("MainWindow: " + str(event) + " " + str(event.type()))
    return QMainWindow.event(self, event)

    class MdiArea(QMdiArea):
    def init(self, parent = None):
    QMdiArea.init(self, parent)
    def event(self, event):
    print2("MdiArea: " + str(event) + " " + str(event.type()))
    return QMdiArea.event(self, event)

    class MdiSubWindow(QMdiSubWindow):
    def init(self, parent = None, flags = Qt.WindowFlags(0)):
    QMdiSubWindow.init(self, parent, flags)
    def event(self, event):
    print2("MdiSubWindow: " + str(event) + " " + str(event.type()))
    return QMdiSubWindow.event(self, event)
    def keyPressEvent(self, event):
    return QMdiSubWindow.keyPressEvent(self, event)
    #def focusInEvent(self, event):
    #self.widget().setFocus(Qt.OtherFocusReason)
    #event.accept()
    #event.reason() #Qt.FocusReason

    def main():
    app = QtGui.QApplication(sys.argv)
    mainWindow = MainWindow()

    mainWindow._mdiArea = MdiArea(mainWindow)
    mainWindow._mdiArea.setOption(QMdiArea.DontMaximizeSubWindowOnActivation)
    mainWindow.setCentralWidget(mainWindow._mdiArea)
    
    wizard = Wizard2(mainWindow)
    wizard.addPage(WizardPage2(wizard))
    
    mdiSubWindow = MdiSubWindow()
    mdiSubWindow.setWidget(wizard)
    mainWindow._mdiArea.addSubWindow(mdiSubWindow)
    
    mainWindow.show()
    sys.exit(app.exec_())
    

    if name == 'main':
    main()
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.